Monday, 25 May 2015

Read and Update the SharePoint Web Part Properties present in page using Server Object Model C#

Problem Description:

We have to read and Update the Custom Webpart properties which we have created on the webpart.cs. Hope you are aware how the Custom Webpart properties created thats why you came to this page to know about how you can read  and update the custom Webpart Properties.

Custom properties
So if you see the above picture i have created a Custom Properties called the "Container ID" Whose value right now is "ABC". We have to read the Container ID in some other Page with the Help of Webpart ID and  Page URl where the Webpart is associated.


Solution:

So at first i have created a Custom Edit menu for the webpart that you can learn from my previous post . I have associated the Page URL and Webpart ID with custom Menu, on click of the custom menu it open an application Page. On Page Load of the Page I read the PageURL and Webpart ID as query string.

///
Page Load to get the Webpart properties
/// 

 protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                GetWebParts();
            }

        }

///
 get the Webpart properties using the WebpartID and PageURL
/// 
  private void GetWebParts()
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    string strWebPartID = Convert.ToString(Request.QueryString["WebPartID"]);
                    string strPageURL = Convert.ToString(Request.QueryString["PageURL"]);
                    SPFile file = web.GetFile(strPageURL);
                    using (SPLimitedWebPartManager wpm = file.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
                    {
                        foreach (System.Web.UI.WebControls.WebParts.WebPart wp in wpm.WebParts)
                        {
                            if (wp is QlistView)// QlistView is a Webpart class where you have created the Custom Webpart Properties
                            {
                                if (strWebPartID.Contains(wp.ID))
                                {
                                    QlistView wp1 = wp as QlistView;// QlistView is a Webpart class where you have created the Custom Webpart Properties


                                    txtContainerID.Text = wp1.ContainerID; // txtContainerID is part of the Page UI
                                
                                    wpm.SaveChanges(wp);
                                }
                                /// ...
                            }
                         

                        }
                    }
                }
            }
        }
///
Below method is used to Update the Custom Webpart Properties
///
  protected void btnSaveUpper_Click(object sender, EventArgs e) // btnsaveUpper is a button on the Page UI
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    string strWebPartID = Convert.ToString(Request.QueryString["WebPartID"]); 
                    string strPageURL = Convert.ToString(Request.QueryString["PageURL"]);
                    SPFile file = web.GetFile(strPageURL);
                    using (SPLimitedWebPartManager wpm = file.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
                    {
                        foreach (System.Web.UI.WebControls.WebParts.WebPart wp in wpm.WebParts)
                        {
                            if (wp is QlistView)// QlistView is a Webpart class where you have created the Custom Webpart Properties
                            {
                                if (strWebPartID.Contains(wp.ID))
                                {
                                    QlistView wp1 = wp as QlistView;// QlistView is a Webpart class where you have created the Custom Webpart Properties

                                    wp1.ContainerID = txtContainerID.Text;
                                
                                   
                                    wpm.SaveChanges(wp);
                                }
                                /// ...
                            }
                          
                        }
                    }
                }
            }
        }

By using the above method you can read and Update the Custom webpart Properties in SharePoint 2010. Let me know if you have any more query on this. Ready to accept your suggestion.

No comments:

Post a comment