SharePoint 2010 Upgrade Solution – Upgrading from SharePoint 2007 to SharePoint 2010 Part 2 – Handle Missing Web Parts

2 ביוני 2011

Upgrade proccess from SharePoint 2007 to SharePoint 2010 include running preupgradecheck tool,


this tool create report, that contain potential issues that can prevent upgrading from SharePoint 2007


to SharePoint 2010.  The one from those issues is missing Web Parts in you environment, those web


part may be missing by many reason, for example: uncorrent uninstall some web parts solution,


previous migration proccess from SharePoint 2003 and etc..


For example you can receive such report.


  • Id = 8c21d9e7-b788-0a69-b08b-d97f59ab5b93, Type = Unknown, Reference = 28, Status = Missing
  • Id = c9cc33de-c985-ef33-f124-ce539263d419, Type = Unknown, Reference = 1, Status = Missing
  • Id = a7563a72-5661-4571-de0c-b2bc1de8718d, Type = Unknown, Reference = 2, Status = Missing
  • Id = d05c0192-9cc9-3e35-7178-c7299151820d, Type = Unknown, Reference = 5, Status = Missing
  • Id = 2526446b-b48f-e401-0106-b98eb4df52ca, Type = Unknown, Reference = 3, Status = Missing
  • Id = 93fc0f26-8918-57e2-2aeb-f9a9154aded7, Type = Unknown, Reference = 26, Status = Missing
  • Id = 57c86f7e-d76c-4394-a011-97683d2b71b4, Type = Unknown, Reference = 2, Status = Missing
  • Id = dd862b60-41b4-0ecd-32e6-dbf6298b31d2, Type = Unknown, Reference = 1, Status = Missing
  • Id = ac697039-7a14-b80b-8815-80574e9b4ef1, Type = Unknown, Reference = 7, Status = Missing
  • Id = 85dc2917-0216-c5ef-e765-1c69ed5e7306, Type = Unknown, Reference = 4, Status = Missing
  • Id = 7b8f9d17-8457-c628-e761-88cf2abf4d46, Type = Unknown, Reference = 1, Status = Missing

  • For locate where those web part is placed, you can run this command inside Command Line.


    cd C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN 


    stsadm.exe -o enumallwebs -includewebparts > c:\missingWP.txt


    Created file missingWP.txt contain references for sites where all web part are placed.


    You search in this file by Web Part id where this Web Part are used, but from this file


    you can't know in which page it is placed.


    For solving this problem I write some Windows Form Application that help to locate in


    which pages special Web Part is placed and allow to delete this Web Part from all those


    pages.



    1. This application can show all Web Parts inside SiteCollection, you need fill


    "Site Url" and press "Get Site Web Parts".


    2. This application can show all Web Parts in specific page (opened and closed),


    you need fill "Site Url" and "Page Url" for example "/ProjectCenter/Pages/default.aspx" 


    and press "Get Pages Web Parts".


    3. This application cand show all pages for specific Web Part, you need fill "Site Url"


    and "Web Part ID" and press "Get Web Parts Pages", inside Grid Control you


    will see all pages where this Web Part is placed.  Here code that retreive and fill Grid Control


    using (SPSite site = new SPSite(siteUrl))
    {
          string conString = site.WebApplication.ContentDatabases[0].DatabaseConnectionString;


          DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient"); 


          using (DbCommand selectCommand = factory.CreateCommand())
          {
                 using (DbConnection connection = factory.CreateConnection())
                 {
                       connection.ConnectionString = conString;
                       connection.Open();
                       DataTable dtData = new DataTable();
                       string commandText = string.Format(@"SELECT WebParts.tp_WebPartTypeId 'WebPartTypeId', WebParts.tp_ID 'WebPartId', AllDocs.DirName 'DirName', AllDocs.LeafName 'LeafName', Webs.Title 'WebTitle', Webs.FullUrl 'WebFullUrl', Sites.PortalURL, WebParts.tp_DisplayName FROM WebParts INNER JOIN Sites 
    ON Sites.Id = WebParts.tp_SiteId INNER JOIN AllDocs ON  AllDocs.Id = WebParts.tp_PageUrlID
    INNER JOIN Webs ON Webs.Id = AllDocs.WebId where WebParts.tp_WebPartTypeId = '{0}'", txtWebPartID.Text);


                       selectCommand.CommandText = commandText;
                       selectCommand.Connection = connection;
                       using (DbDataReader rdr = selectCommand.ExecuteReader())
                       {
                              dtData.Load(rdr);
                              rdr.Close();
                        }


                       dataGridView1.DataSource = dtData;
                }
          }


    After this you can press "Delete" button and the application will delete all reference for this Web Part.


    Here code that delete all reference for this Web Part.


    DataTable dt = (DataTable)dataGridView1.DataSource;
    if (dt == null || dt.Rows.Count == 0) return;


    SPSecurity.RunWithElevatedPrivileges(delegate()
    {


        using (SPSite site = new SPSite(txtSiteUrl.Text))
        {
              string webUrl = string.Empty;
              string pageUrl = string.Empty;
              string folderUrl = string.Empty;
              string webPartID = string.Empty;
              PublishingPage publishingPage = null;
              foreach (DataRow row in dt.Rows)
              {
                   if (row["LeafName"] == DBNull.Value ||                                
                       row["WebPartId"] == DBNull.Value)
                       continue;


                   webUrl = "/";
                   if (row["WebFullUrl"] != DBNull.Value && row["WebFullUrl"].ToString() != string.Empty)
                       webUrl = row["WebFullUrl"].ToString();


                   using (SPWeb web = site.OpenWeb(webUrl))
                   {
                         folderUrl = string.Empty;
                         if (row["DirName"] != DBNull.Value && row["DirName"].ToString() != string.Empty)
                            folderUrl = "/" + row["DirName"].ToString();
                         pageUrl = string.Format("{0}/{1}", folderUrl, row["LeafName"].ToString());
                         SPFile file = web.GetFile(pageUrl); // or what ever page you are interested in    


                         if (file.InDocumentLibrary && PublishingPage.IsPublishingPage(file.Item))
                         {
                             if (file.Level == SPFileLevel.Checkout)
                                 file.UndoCheckOut();


                              publishingPage = PublishingPage.GetPublishingPage(file.Item);
                              publishingPage.CheckOut();
                          }


                          using (SPLimitedWebPartManager wpm = file.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
                          {
                                 List<System.Web.UI.WebControls.WebParts.WebPart> WebPartsToDelete = new List<System.Web.UI.WebControls.WebParts.WebPart>();


                                  foreach (System.Web.UI.WebControls.WebParts.WebPart wp in wpm.WebParts)
                                  {
                                         webPartID = wp.UniqueID.ToString();
                                          if (webPartID.IndexOf("g_") != -1)
                                              webPartID = webPartID.Substring(webPartID.IndexOf("g_") + 2);
                                          webPartID = webPartID.Replace("_", "-");
                                          if (row["WebPartId"].ToString().ToLower() == webPartID)
                                          {
                                                WebPartsToDelete.Add(wp);
                                          }
                                         else if (wp.IsClosed)
                                         {
                                              WebPartsToDelete.Add(wp);
                                         }
                                    }


                                   if (WebPartsToDelete.Count > 0)
                                   {
                                         foreach (System.Web.UI.WebControls.WebParts.WebPart wp in WebPartsToDelete)
                                         {
                                                wpm.DeleteWebPart(wp);
                                         }
                                    }
                              }
                              file.Update();
                              if (file.InDocumentLibrary && PublishingPage.IsPublishingPage(file.Item))
                              {
                                    file.CheckIn(string.Empty);
                                    file.Publish(string.Empty);
                               }
                               web.AllowUnsafeUpdates = true;
                               web.Update();
                        }
                   }


              }
     });


    Here you can download binary code for this application. WebPartView.zip


    I hope this application help you solve "Missing Web Parts" problem in upgrading proccess.


    Enjoy.


     

    הוסף תגובה
    facebook linkedin twitter email

    כתיבת תגובה

    האימייל לא יוצג באתר. שדות החובה מסומנים *