Tuesday, May 15, 2012

How Modify MasterPage or Pagelayout programmatically? (Especially when it is in use)

Most of the time we are using SharePoint Designer for master page and page layout deployment, which is fine for first time going live because for the first time we mostly used backup restore or other stuff.

But problem occurs when you want to update master page or page layout in subsequent release. We can do that easily with SharePoint designer but some unlucky fellow like us who do not have access to production will have to find some workaround for that. And if master page and Page Layout  being used then it will not allow to delete and add it again.

Here are simple steps what we had done

  • Add physical copy of master page/page layout in your project (or put it in 14 hive)
  • Create a feature receiver which will read that file added in 14 hive, read as binary stream.
  • Find master page in master page gallery (_catalog folder)
  • If (not found) then add new master page/layout (will be used for first time deployment)
  • If (found) then open/read master page/layout from library
  1. Check out
  2. Open as binary form and copy binary object from physical copy
  3. Commit transection, so new version will be added for master page/layout.
  4. Check in
  5. Publish
This way if master page is currently used in site or page layout has been used any of the page you can easily update it from code.

 It will not require access to production. You can add feature receiver in WSP and activate it on deployment; and you are done.

We will provide code snippet for this soon :)

1 comment:

Anonymous said...

Please provide the code snippet for the above requirement.

Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com