locked
Custom Web part overriding the custom web part property RRS feed

  • Question

  • Hi folks,

    I have written a code to display site quota on each site collection. 
    For site A, the custom web property which is first site URL works fine. When I place the same web part for site B,I wanted to show site quota of Site B only but shows site Aquota.

    I am using static variable to pass the custom web part property to display on the page which could be issue. Any help would be greatly appreciated.  

    This is my code. 

    1. VisualWebPart.cs 
    namespace DisplaySizeQouta.VisualWebPart1
    {
        [ToolboxItemAttribute(false)]
        public class VisualWebPart1 : WebPart
        {
    
            //Declare Custom Property 
             public static string _value;
            [System.Web.UI.WebControls.WebParts.WebBrowsable(true),
             System.Web.UI.WebControls.WebParts.WebDisplayName("Enter the site collection"),
             System.Web.UI.WebControls.WebParts.WebDescription(""),
             System.Web.UI.WebControls.WebParts.Personalizable(
             System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared),
             System.ComponentModel.Category("Custom Properties"),
             System.ComponentModel.DefaultValue("")]
    
            public string _Value
            {
                get { return _value; }
                set { _value =value; }
            }
    
    
            // Visual Studio might automatically update this path when you change the Visual Web Part project item.
            private const string _ascxPath = @"~/_CONTROLTEMPLATES/15/DisplaySizeQouta/VisualWebPart1/VisualWebPart1UserControl.ascx";
    
        
            protected override void CreateChildControls()
            {
                Control control = Page.LoadControl(_ascxPath);
                Controls.Add(control);
              
            }
        }
    }
    
    

     2. VisualWebPartUserControl.cs 

    namespace SizeQouta.VisualWebPart1
    {
        public partial class VisualWebPart1UserControl : UserControl
        {
    
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
    
                lblSite.Text = VisualWebPart1._value;
                string checkSite = VisualWebPart1._value;
                //Check the lblSite does not return null
    
    
    #region Check if lblSite Exists 
                
    
                if (!string.IsNullOrEmpty(checkSite)) //Check if statement has no null value
                {
    
                    //Check if the site collection exists
                    Uri checkSiteExists = new Uri(VisualWebPart1._value);
                    if (SPSite.Exists(checkSiteExists))
                    {
                       // lblCheckSite.Text = "Site Exists";
                    }
                    else
                    {
                        this.Controls.Add(new LiteralControl("<div style='color:Red'><p><strong>Sorry site does not exist!</strong></p></div>"));
                    }
                }
                 
    #endregion 
    
                #region Site Quota
                if (!string.IsNullOrEmpty(checkSite)) //Check if statement has no null value
                {
    
                    //Check if the site collection exists
                    Uri checkSiteExists = new Uri(VisualWebPart1._value);
                    if (SPSite.Exists(checkSiteExists))
                    {
                        using (SPSite site = new SPSite(VisualWebPart1._value))
                        {
    
                            //long storageUsed = site.Usage.Storage / (1024 * 1024 );
                            //long storageUsedMax = site.Quota.StorageMaximumLevel / (1024 * 1024);
    
    
    
                            long storageUsed = Convert.ToInt64(site.Usage.Storage);
                            long storageUsedMax = Convert.ToInt64(site.Quota.StorageMaximumLevel);
    
                         
                            float usedF = (float)storageUsed;
                            float maxF = (float)storageUsedMax;
                            float percentage = (usedF / maxF) * 100;
    
                            decimal round = Math.Round((decimal)usedF, 0);
    
                          lblSizeQuota.Text = "Site storage " +  usedF.ToString(); " MB"
                          lblSizeQuotaMax.Text = "out of " + maxF.ToString()  + "MB Quota";
                                 
                            
                            
                        }
    
                    }
                   
                   
                }
                 
                #endregion 
            }



    Sandy

    Saturday, June 4, 2016 4:39 PM

Answers

  • Hi Sandy,

    Please modify the following line of code in your project: “public static string _value;” to “public string _value;”.

    Remove “static” in that line and then test again.

    Best Regards,

    Victoria

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


    Monday, June 6, 2016 11:35 AM
  • Thanks for Justin and Victoria, 


    @Justin: sadly, you have not seen the code that I provided in my web part code. It is clearly written that I am getting from server side code to fetch and display the quota value.

    @Victoria: I tried that option as well, but I deploy I cant see my custom web part property as all :(. 

    Finally, I found my solution myself. 

    1. VisualWebPart.cs 

        [System.Web.UI.WebControls.WebParts.WebBrowsable(true),
            System.Web.UI.WebControls.WebParts.WebDisplayName("Enter the value"),
            System.Web.UI.WebControls.WebParts.WebDescription(""),
            System.Web.UI.WebControls.WebParts.Personalizable(
            System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared),
            System.ComponentModel.Category("Custom Properties"),
            System.ComponentModel.DefaultValue("")]
            public string siteURL
            {
                get;
                set;
            } 



    2. 
    VisualWebPartUserControl.cs 

     public VisualWebPart1 MyWebPart
            {
                get;
                set;
            }

    After I deployment, I can see Site A, Site B and SIte C fetching correct site quota for their respect quota assigned.  
    Thanks a ton for everyone for your suggestions and providing valuable information! 

     


    Sandy

    Tuesday, June 7, 2016 11:43 PM

All replies

  • Hi Sandy,

    Please change the public static value to private static value. This would fix your issue.


    Please remember to click &#39;Mark as Answer&#39; on the answer if it helps you

    Saturday, June 4, 2016 5:14 PM
  • Thanks Lakshman, 

    I tried your change. But when added my custom web part to Site B and Site C, it still showed the value of Site A. 
    So no change to the code. :( 


    Sandy

    Saturday, June 4, 2016 5:42 PM
  • Have you change all your public variables to private static. It should work. its working for me. Please compile it and deploy again?

    Please remember to click &#39;Mark as Answer&#39; on the answer if it helps you

    Saturday, June 4, 2016 6:18 PM
  • Hi Lakshmanan, 

    Thanks for your time. I have only 1 static variable which I already changed and complied. 
    Can you please share the working code which you mentioned in your reply?  It would be useful for me. 

    Thank you. 


    Sandy

    Saturday, June 4, 2016 6:31 PM
  • if it always show quota of site A, there must be hard code in your code.

    顺其自然地勇往直前!—Justin Liu

    Monday, June 6, 2016 1:32 AM
  • Hi Sandy,

    Please modify the following line of code in your project: “public static string _value;” to “public string _value;”.

    Remove “static” in that line and then test again.

    Best Regards,

    Victoria

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


    Monday, June 6, 2016 11:35 AM
  • Thanks for Justin and Victoria, 


    @Justin: sadly, you have not seen the code that I provided in my web part code. It is clearly written that I am getting from server side code to fetch and display the quota value.

    @Victoria: I tried that option as well, but I deploy I cant see my custom web part property as all :(. 

    Finally, I found my solution myself. 

    1. VisualWebPart.cs 

        [System.Web.UI.WebControls.WebParts.WebBrowsable(true),
            System.Web.UI.WebControls.WebParts.WebDisplayName("Enter the value"),
            System.Web.UI.WebControls.WebParts.WebDescription(""),
            System.Web.UI.WebControls.WebParts.Personalizable(
            System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared),
            System.ComponentModel.Category("Custom Properties"),
            System.ComponentModel.DefaultValue("")]
            public string siteURL
            {
                get;
                set;
            } 



    2. 
    VisualWebPartUserControl.cs 

     public VisualWebPart1 MyWebPart
            {
                get;
                set;
            }

    After I deployment, I can see Site A, Site B and SIte C fetching correct site quota for their respect quota assigned.  
    Thanks a ton for everyone for your suggestions and providing valuable information! 

     


    Sandy

    Tuesday, June 7, 2016 11:43 PM
  • Hi Sandy,

    Did you just remove "static" this word from your previous project?

    From your second project, it seems that you have removed this line of code.

    I am glad that your issue has been resolved. You can mark any reply which is helpful as answer and it will help others who meet the issue in this forum.

    Best Regards,

    Victoria   


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

    Wednesday, June 8, 2016 1:19 AM