Tuesday, May 22, 2007

SPS/WSS 2003 : Overview of custom webparts

There are some steps that you have to take when you want to upgrade your existing 2003 environment to a 2007 environment. One of those steps is to get an overview of all the installed custom webparts. Now this is not that difficult, you just use the STSADM command to retrieve all the installed webparts.
But what you want to know (and this is even not only desirable information to have when you are not upgrading) is, are the custom webparts used by our users? If so, on which site and on which page is the custom webpart dropped?

To find out these answers we use the GetWebPartCollection method from the SPWeb object. This method returns a collection of WebParts. If you take a look at all of the properties of this object there is nothing that truly identifies the webpart. Sure you've got the ID and ClientID but those ID's are unique for the webpage where the webpart is hosted. What I found was that the Description property is the only property that is easily identified and unique (unless you've build all you're webparts with "WebPart1." as the default description :P)

The next thing to overcome was that you use the GetWebPartCollection method with a specifaction of the page (e.g. default.aspx), now this works fine for a default sitetemplate. But if your sites are based upon the workingspace template, the possibility exists that there are more pages on the sites where the webpart could be found. So you've got to enumarate through all the pages as well.

So here's the function:

 

static void GetWebparts (string url)
{
    
    //boolean to determine when we want to add an WebpartItem to the ArrayList
    bool GotWebPart = false;
    
    
    //Opening the site
    SPSite siteCollection = new SPSite(url);
    SPWeb web = siteCollection.OpenWeb();
    
    try
    {
        //Retrieving all the webparts on a particular page. 
        SPWebPartCollection collection = web.GetWebPartCollection("default.aspx", 
            Microsoft.SharePoint.WebPartPages.Storage.Shared);

        foreach (Microsoft.SharePoint.WebPartPages.WebPart webpart in collection)
        {
            //If the webpart has a description like the description that can be found in the .DWP file, we are gonna add this webpart to the collection
            if (webpart.Description == "WhatNew." 
                
                )
            {
                WebpartItem item = new WebpartItem();
                item.OriginalTitle = GetOriginalTitle(webpart.Description);
                item.Webpart = webpart.Title;
                string LinkToSite = web.Url.ToString() + ", " + web.Title.ToString();
                item.Site = LinkToSite;
                item.Page = "default.aspx";
                
                GotWebPart = true;
                
                //If the webtemplate is based on a workspace, we have not only the default.aspx 
                //page but also for every tab a new .aspx file
                if (web.WebTemplate == "MPS")
                {
                    SPFolder srcFolder = web.Folders["pages"];
                    foreach(SPFile file in srcFolder.Files)
                    {
                        SPWebPartCollection pagescollection = 
                            web.GetWebPartCollection("pages/"+file.Name.ToString(), 
                            Microsoft.SharePoint.WebPartPages.Storage.Shared);
                        foreach (Microsoft.SharePoint.WebPartPages.WebPart _webpart in pagescollection)
                        {
                            if (_webpart.Description == webpart.Description)
                            {
                                item.Page += " | " + file.Name.ToString();
                            }
                        }
                        
                    }
        
                }
                if (GotWebPart == true)
                {
                    //Finally we put the item in the ArrayList                        
                    items.Add(item);
                }
            }
        }
    }
    catch (Exception error)
    {
        Console.WriteLine(error.Message.ToString() + "    " + url.ToString());
    }

    finally
    {
        web.Close();
        web.Dispose();

        siteCollection.Close();
        siteCollection.Dispose();
        
    }

}

 

Hope this helps somebody out  :)

 

Technorati tags: , ,

2 comments:

Anonymous said...

How would I remove a webpart from this collection. Is there some remove method

electronic signature said...

Congratulations, your blog is appealing and informative. Going through your Information, I found quite a few new ideas to implement.