0

I am creating a method that will iterate through all of the web parts on a page in my SharePoint site. What I need to do is update a custom web part's, created by another company, property. I have gone through all of the properties for the web part using C# and none of the values returned are what i am looking for.

Here is my C# code,

private void GetWebParts()
    {   
        // What site you are using.
        using (SPSite site = new SPSite("http://site..com/sites/dept/"))
        {
            // Returns the Web site that is located at the specified server-relative or site-relative URL.
            using (SPWeb web = site.OpenWeb())
            {
                // Give the file name you are looking for.
                SPFile file = web.GetFile("SitePages/FNDisposalRequestICV.aspx");
                using (SPLimitedWebPartManager wpm = file.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
                {   
                    // Loop through all of the web parts on the page.
                    foreach (SysUI.WebPart wp in wpm.WebParts)
                    {
                        // Here is where I should be able to get and update all of the 
                        // web part properties. For some reason I am unable to get all 
                        // of the custom web part properties but am able to get the 
                        // "standard" ones i.e. Title, TitleUrl...
                        wp.Title;

                        // Saves the changes of any updates you made to the web part.
                        //wpm.SaveChanges(wp);
                    }
                }
            }
        }
    }

Is there a certain class to use that is used for custom web parts? Or am i missing a step somewhere in my code?

EDIT

I have exported the custom web part where i need to get the data. The Property does exist and has a value. When I go to my C# code and try to access this property, I get an error saying it doesn't exist.

0

You should convert the webpart object to the custom object. First include the dll's which has the custom webpart. Then do as below

CustomWebPart cw = wpm.WebParts[0] as CustomWebPart;

Now you can access the custom properties of that webpart.

  • Amal, Thanks for the answer. I believe this is pointing in the right direction. However, can you please elaborate a little on your answer? – IE5Master Jul 22 '16 at 14:43
  • Ok the custom webpart should be deployed as part of a solution. First find the solution and the respective dll. You can also open up the associated ASCX file to find this information.. then include this dll in your project.. then typecast webpart to the custom webpart.. – Amal Hashim Jul 22 '16 at 14:44
  • The custom web part I am trying to get property values of was developed by a 3rd party and I am unable to locate the project. I do not think I will be able to include the 3rd party's web part into my solution. – IE5Master Jul 22 '16 at 14:57
  • In that case your next option is using Reflection. I didn't quiet understand what is the problem in add a dll reference to your project. Anyway this dll is already deployed into the SP Farm. – Amal Hashim Jul 22 '16 at 14:58
  • The 3rd party does not include dll's when we receive the updated projects from them. I will look into using Reflection more. – IE5Master Jul 22 '16 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.