locked
Call variable from partial class in visual webpart RRS feed

  • Question

  • Hi,

    I have a visual Web part with form.ascx and it's codebehind file. I also have other classes with different namespaces and I have a need to get value from variable in form.ascx.cs. I get string value properly in form.ascx.cs but in Common.cs it's always null. I not real code guy so could you tell me what's wrong here?

    Here's what I have (Simplified)

    Form.ascx.cs:

        

    namespace Company.Sharepoint.Workspace
    {

        public partial class MySolution : WebPart
        {

        private static string MyVar1
        public static string MyVar
        {
        get { return MyVar1; }
        set { MyVar1 = value; }
        }

        protected void Page_load(object sender, EventArgs e)
        {

        SPWeb myWeb = SPContext.Current.Web;

        SPUser user = myWeb.Users["John Landis"];
        MyVar = user.Email;

        }

    Common.cs:

    namespace Company.Sharepoint.Workspace.Files
    {
        public class MyOtherSolution
        {
        public static string MyVar = Company.Sharepoint.Workspace.MyVar;
        public void GetMail(string mail)
        {

        mail = MyVar;

        }

    Thanks for your help!!


    Antti Astikainen

    Tuesday, October 18, 2016 4:58 PM

Answers

  • Hi,

    I resolved this issue other way. I stored string from form to SharePoint listItem and then read it in common class. I found out that this was also more reliable way to do it in my solution.

    Thanks for help anyway :)

    Br,  Antso


    Antti Astikainen

    • Marked as answer by Patrick_Liang Thursday, October 27, 2016 1:52 AM
    Monday, October 24, 2016 1:16 PM
  • in case if someone else will face with similar issue: in example above web part property is declared as static. According to C# rules static variables of the class (web part class Company.Sharepoint.Workspace.MySolution in this example) are initialized before 1st use of the class. E.g. if you make app pool recycle and after that in your code you try to use Company.Sharepoint.Workspace.MySolution.MyVar from another class Company.Sharepoint.Workspace.Files.MyOtherSolution this variable will be initialized with default value - which is null for strings.

    If after that you will go to web part page, edit page, set Company.Sharepoint.Workspace.MySolution.MyVar by changing appropriate web part property and then try to use it in Company.Sharepoint.Workspace.Files.MyOtherSolution - this time it will contain correct value. BUT the same issue will happen again after next app pool recycle.

    More over if you use farm with several front end servers - Company.Sharepoint.Workspace.MySolution.MyVar may be null in Company.Sharepoint.Workspace.Files.MyOtherSolution even after editing of web part property.

    Summary from all of that - is that don't use static web part properties unless you fully aware how they work. If you need to reuse value which is changed via web part property you should save it to some persistence storage. Author of the topic used list item which is suitable, it also can be stored e.g. in property bag. There is also way to get value from non-static web part property - but in order to do this you need to read web part property programmatically via SPLimitedWebPartManager like shown e.g. here: Update web part properties programatically.


    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Monday, October 24, 2016 1:49 PM

All replies

  • Hi,

    It seems you didn’t show the correct code.

    If the Common.cs is a public/help class, you could declare the method as static and call in webpart solution.

    public class MyOtherSolution
            {            
                public static void GetMail(string mail)
                {
                    Console.WriteLine("common class");
                }
            }
    
    protected void Page_load(object sender, EventArgs e)
                {
    
                    SPWeb myWeb = SPContext.Current.Web;
    
                    SPUser user = myWeb.Users["John Landis"];
                    MyOtherSolution.GetMail(user.Email);
    
                }

    Best Regards,

    Lee


    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, October 19, 2016 3:21 AM
  • Hi,

    sorry but this is not answering to my problem. I probably wasnt accurate enough:

    So here's my first class from other namespace:

    FORM.ASCX.CS

    namespace Company.Sharepoint.Workspace
    
        public partial class WorkspaceOrderFormUserControl
        {
            private static string strEmail1;
            public static string strEmail
            {
                get { return strEmail1; }
                set { strEmail1 = value; }
            }
    
            public void Page_Load(object sender, EventArgs e)
            {
                strEmail = "John Landis";


    COMMON.CS

    namespace Company.Sharepoint.Workspaces.Files { public partial class Common { public static void userMail(string mail) {

    HOW TO GET strEmail value HERE FROM FORM.ASCX.CS?




    Antti Astikainen

    Wednesday, October 19, 2016 7:01 AM
  • Hi,

    As the strEmail was set at run time for your case, so you can’t get it directly.

    What’s the purpose for the design?/ When you need get this strEmail?

    Best Regards,

    Lee


    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, October 19, 2016 7:44 AM
  • Hi!

    Technically you can use  Company.Sharepoint.Workspace.WorkspaceOrderFormUserControl.strEmail to get the value but I don't recommend this approach. Generally, static properties are evil so you should consider changing your code to remove "static" and then somewhere in your code you may call:

    Common.userMail(theOrderUserControlInstance.strEmail);


    If my suggestion helped you to solve your problem, please don't forget to mark it as Answer

    Wednesday, October 19, 2016 7:51 AM
  • Hi,

    Is any update for your issue?

    Best Regards,

    Lee


    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, October 24, 2016 9:44 AM
  • Hi,

    I resolved this issue other way. I stored string from form to SharePoint listItem and then read it in common class. I found out that this was also more reliable way to do it in my solution.

    Thanks for help anyway :)

    Br,  Antso


    Antti Astikainen

    • Marked as answer by Patrick_Liang Thursday, October 27, 2016 1:52 AM
    Monday, October 24, 2016 1:16 PM
  • in case if someone else will face with similar issue: in example above web part property is declared as static. According to C# rules static variables of the class (web part class Company.Sharepoint.Workspace.MySolution in this example) are initialized before 1st use of the class. E.g. if you make app pool recycle and after that in your code you try to use Company.Sharepoint.Workspace.MySolution.MyVar from another class Company.Sharepoint.Workspace.Files.MyOtherSolution this variable will be initialized with default value - which is null for strings.

    If after that you will go to web part page, edit page, set Company.Sharepoint.Workspace.MySolution.MyVar by changing appropriate web part property and then try to use it in Company.Sharepoint.Workspace.Files.MyOtherSolution - this time it will contain correct value. BUT the same issue will happen again after next app pool recycle.

    More over if you use farm with several front end servers - Company.Sharepoint.Workspace.MySolution.MyVar may be null in Company.Sharepoint.Workspace.Files.MyOtherSolution even after editing of web part property.

    Summary from all of that - is that don't use static web part properties unless you fully aware how they work. If you need to reuse value which is changed via web part property you should save it to some persistence storage. Author of the topic used list item which is suitable, it also can be stored e.g. in property bag. There is also way to get value from non-static web part property - but in order to do this you need to read web part property programmatically via SPLimitedWebPartManager like shown e.g. here: Update web part properties programatically.


    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Monday, October 24, 2016 1:49 PM