Thursday, 27 February 2014

Duplicate Web Parts issue while creating new page


Hi again,
Today something regarding one of the new feature of SharePoint 13.  There is new attribute “ReplaceContent=TRUE” to the <File> element in <Module>.  

Till the SharePoint 2010, whenever we reactivate PageLayout feature (feature which provisions the page layouts in master page gallery), web parts which are added on page layout get duplicated on newly created page and we do not have very standard solution to resolve this problem except writing feature receiver and remove duplicated web parts.

In SharePoint 2013 new attribute “ReplaceContent=TRUE” is there to <File> element, and if we used this while provisioning page layout then it overrides the existing web parts on the page layouts. So whenever we create new page from the given page layouts, web parts are not get duplicated.

Example:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

  <Module Name="MyPageLayouts" Url="_catalogs/masterpage" RootWebOnly="TRUE">

    <File Path="MyPageLayouts\MyPageLayout.aspx" Url="MyPageLayouts.Intranet/MyPageLayout.aspx" Type="GhostableInLibrary" ReplaceContent="TRUE">

<!—- All our web parts goes here -->

    </File>

  </Module>

Have a look in above example, I have set the ReplaceContent=”TRUE”. Because of this whenever I have change or add/delete web part in this elements.xml file and I need to reactivate the feature, web parts on page layouts are not get duplicated but they are get overridden.

Other Usage example:

I’ll like to share one more scenario related to this, I have sandbox solution and which I am deploying on Office 365. I was uploading my package file (.wsp) in Solution Gallery and activating it. In my package I have one feature which deploying my css file in master page gallery since I do not have file system access. I had a change in css file, I did the change, packaged the solution, deactivated the solution first, uploaded in solution gallery and activated it. But my css changes are not reflected. Then I had thought of “ReplaceContent” attribute and I have used it in module element while provisioning the css file and wonder happen, it worked like a charm.

Feel free if you have some thoughts on this or please share your experience.

Thanks!
Enjoy reading J

Feel free to comment / feedback if any or if you have any query J

5 comments:

Anonymous said...

Awesome, Thanks to SP 2013 and to you for sharing.

Anonymous said...

Thank you so much for the fix. it worked fine :-)

Anonymous said...

does not work for me...webparts still getting duplicated...

Caroline said...

Awesome, that worked for me. Thanks for sharing!

Prasham Sabadra said...

Welcome! Glad to hear it helped.