Thursday, September 9, 2010

How to extract properties of webpart from any site.

We design a toolpane for webpart by adding a lot of toolparts into toolpane. We can design any layout as we want, but with the width of toolpane it's really difficult to design a complexity interface, so I want to introduce a new layouts  that can be designed very complexity.


In this topic, I give you a default security that user can open site with the currently right.

With a original toolpane, just add a button named "Modify Web Part" and after click on that button there is a new window appear and show your layouts need to define everything as needed. In this topic, assume that the  page to modify toolpane is called "modify.aspx".

This page, we need to know which webpart need to modify, so we will send to modify.aspx two parameters as query string [page, wpGuid]. In onclick event of "Modify Web Part" button, we could open a window like this:

window.open("modify.aspx?page=http://server/test/site.aspx&wpGuid=8230-12344-1111111xxxxx");


To extract all properties of web part, we need to get web part object first, here is the code:





public MyWebPart GetWebPart()
{
     try
     {
       string page = Convert.ToString(this.Page.Request.QueryString["page"]);

       string wpGuid = Convert.ToString(this.Page.Request.QueryString["wpGuid"]);

       if (page != "" && wpGuid != "")
       {
          using (SPSite site = new SPSite(page))
          {
              using (SPWeb web = site.OpenWeb())
              {
                  using (SPLimitedWebPartManager pageWebParts = web.GetLimitedWebPartManager(page, System.Web.UI.WebControls.WebParts.PersonalizationScope.User))
                  {
                      try
                      {
                         SPLimitedWebPartCollection webParts = pageWebParts.WebParts;
                         foreach (WebPart wpart in webParts)
                         {
                            if (wpart.StorageKey.ToString().Equals(wpGuid))
                            {
                                 return (MyWebPart)wpart;
                             }
                         }
                       }
                       finally
                       {
                            pageWebParts.Web.Dispose();
                       }
                    }
                }
           }
         }
       }
       catch 
       {
                //your exception
       }
       return null;
   }

But in some case, the [Page] parameter is incorrect format, sometime it would be missing /default.aspx or page.apsx or any prefix.aspx, and the code above will not work. So, we need to identify the url to pass it for SPSite object. The code bellow is to show what we need to do:




public MyWebPart GetWebPart()
        {
            try
            {
                string page = Convert.ToString(this.Page.Request.QueryString["page"]);

                string wpGuid = Convert.ToString(this.Page.Request.QueryString["wpGuid"]);

                if (page != "" && wpGuid != "")
                {
                    using (SPSite site = new SPSite(page))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            using (SPLimitedWebPartManager pageWebParts = web.GetLimitedWebPartManager(page, System.Web.UI.WebControls.WebParts.PersonalizationScope.User))
                            {
                                try
                                {
                                    SPLimitedWebPartCollection webParts = pageWebParts.WebParts;
                                    foreach (WebPart wpart in webParts)
                                    {
                                        if (wpart.StorageKey.ToString().Equals(wpGuid))
                                        {
                                            return (MyWebPart)wpart;
                                        }
                                    }
                                }
                                finally
                                {
                                    pageWebParts.Web.Dispose();
                                }
                            }
                        }
                    }
                }
            }
            catch // it throw exception when the configuration server is http/https
            {
                return GetWebPartAgain();
            }
            return null;
        }

private MyWebPart GetWebPartAgain()
        {
            string url = Convert.ToString(this.Page.Request.QueryString["page"]);

            string wpGuid = Convert.ToString(this.Page.Request.QueryString["wpGuid"]);

            using (SPSite site = new SPSite(url))
            {

                using (SPWeb web = site.OpenWeb())
                {
                    SPFileCollection files = web.Files;

                    foreach (SPFile file in files)
                    {
                        using (SPLimitedWebPartManager pageWebParts = file.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.User))
                        {
                            try
                            {
                                SPLimitedWebPartCollection webParts = pageWebParts.WebParts;
                                foreach (WebPart wpart in webParts)
                                {
                                    if (wpart.StorageKey.ToString().Equals(wpGuid))
                                    {
                                         return (MyWebPart)wpart;
                                    }
                                }
                            }
                            finally
                            {
                                pageWebParts.Web.Dispose();
                            }
                        }
                    }
                }
            }
            return this.Context.Request.Url.ToString();
        }

When you get the Webpart object, you can extract its properties and work anything on that. After changes, you can serialize all changes into an xml string and return the function called the page modify.aspx. A hidden field will catch this xml string and store into a property of toolpart. Every time open modify.aspx, you can deserialize to work on that.

Good luck to you.

No comments: