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.

Create Custom Webpart Menu item ( Verbs ) like Edit web part in SharePoint 2013 using C#


Problem Description:

We have the requirement to add new webpart menu like below pic. On click of the Custom menu we have to associate to a Javascript which will open SharePoint popup page.


Solution : 

I searched a lot on google try to figure out how we can add custom menu to the webpart with the Keyword "Create Custom Webpart Menu in SharePoint 2010" . but i could not find any result on first two three pages of google Result. finally i found in some page this webpart menu items are called as Webpart Verbs. Once i get this (Verbs)  Keyword it was easy for me to get the piece of code from google. I am just writing this if someone like me doesn't know about the Webpart Verbs , for them it might be useful.

Code :

Add the below code in your Webpart.cs file, it should create a Custom menu as shown in above figure.

   public override WebPartVerbCollection Verbs
        {
            get
            {
               
                var yourVerb = new WebPartVerb(ID + "yourVerb ", "showModel('"+ID+"')") { Text = "Edit List View webpart" };
               
                var verbs = new[] { yourVerb };
                return new WebPartVerbCollection(base.Verbs, verbs);
            }

        }

We have to Override the WebpartCollection Verbs property and add new custom verb to the collection. In the Above method, i have "showModel" Javascript method which will be called on click of the Menu "Edit List View webpart". You can write either server side code or Java script function against the custom menu.

Hope this article will help to create a custom menu in the webpart.

Ask me if you have any doubt.