Pages

Search This Blog

Saturday, December 3, 2011

Uncustomized the PageLayout or Page via SharePoint API


Sometimes we have a requirement where we need to remove all customization from the PageLayout or from the Page. OOB SharePoint provide link to reset to site definition. But is someone needs to do that via code then below code will help them.

SPSecurity.RunWithElevatedPrivileges(delegate()
                      {
                          using (SPSite site = new SPSite("http://sharepoint2010"))
                          {
                              PublishingSite publishingSite = new PublishingSite(site);
                              PageLayoutCollection pageCollection = publishingSite.PageLayouts;
                              foreach (PageLayout layout in pageCollection)
                              {
                                  SPFile currentFile = site.RootWeb.GetFile(layout.ServerRelativeUrl);
                                  if (currentFile.CustomizedPageStatus == SPCustomizedPageStatus.Customized || currentFile.CustomizedPageStatus == SPCustomizedPageStatus.None)
                                  {
                                      try
                                      {
                                          currentFile.RevertContentStream();
                                          currentFile.Versions.DeleteAll();
                                          currentFile.Update();
                                          site.RootWeb.Update();
                                      }
                                      catch (Exception ex)
                                      {
                                          Console.WriteLine("Error occuured : " + ex.Message);
                                      }

                                  }
                              }

                          }
                      });

Hope it will helps!!! 

No comments:

Post a Comment