Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

While creating a new publishing site I would like to add the shared documents webpart on the default page. I've read a lot of documentation, but haven't found any answer yet.

This is the ONET.xml file

<Project Title="$Resources:onet_TeamWebSite;" Revision="2" ListDir="$Resources:core,lists_Folder;" xmlns:ows="Microsoft SharePoint" UIVersion="4">
  <NavBars>
   </NavBars>

  <Configurations>
    <Configuration ID="0" Name="Blank" MasterUrl="_catalogs/masterpage/v4.master">
      <Modules>
        <Module Name="DefaultBlank" />
      </Modules>
      <Lists>
        <List FeatureId="00BFEA71-E717-4E80-AA17-D0C71B360101" Type="101" Title="$Resources:core,shareddocuments_Title;" Url="$Resources:core,shareddocuments_Folder;" />
        <List FeatureId="00BFEA71-6A49-43FA-B535-D15C05500108" Type="108" Title="$Resources:core,discussions_Title;" Url="$Resources:core,lists_Folder;/$Resources:core,discussions_Folder;" QuickLaunchUrl="$Resources:core,lists_Folder;/$Resources:core,discussions_Folder;" />
        <List FeatureId="00BFEA71-D1CE-42de-9C63-A44004CE0104" Type="104" Title="$Resources:core,announceList;" Url="$Resources:core,lists_Folder;/$Resources:core,announce_Folder;">
          <Data>
            <Rows>
              <Row>
                <Field Name="Title">$Resources:onetid11;</Field>
                <Field Name="Body">$Resources:onetid12;</Field>
                <Field Name="Expires">&lt;ows:TodayISO/&gt;</Field>
              </Row>
            </Rows>
          </Data>
        </List>
        <List FeatureId="00BFEA71-2062-426C-90BF-714C59600103" Type="103" Title="$Resources:core,linksList;" Url="$Resources:core,lists_Folder;/$Resources:core,links_Folder;" />
        <List FeatureId="00BFEA71-EC85-4903-972D-EBE475780106" Type="106" Title="$Resources:core,calendarList;" Url="$Resources:core,lists_Folder;/$Resources:core,calendar_Folder;" QuickLaunchUrl="$Resources:core,lists_Folder;/$Resources:core,calendar_Folder;/Calendar.aspx" />
        <List FeatureId="00BFEA71-A83E-497E-9BA0-7A5C597D0107" Type="107" Title="$Resources:core,taskList;" Url="$Resources:core,lists_Folder;/$Resources:core,tasks_Folder;" />
      </Lists>


      <SiteFeatures>

        <!-- BasicWebParts Feature -->
        <Feature ID="00BFEA71-1C5E-4A24-B310-BA51C3EB7A57" />

        <!-- Three-state Workflow Feature -->
        <Feature ID="FDE5D850-671E-4143-950A-87B473922DC7" />

        <!--  Site collection Publish features -->
        <Feature ID="f6924d36-2fa8-4f0b-b16d-06b7250180fa" />

      </SiteFeatures>
      <WebFeatures>

        <!-- TeamCollab Feature -->
        <Feature ID="00BFEA71-4EA5-48D4-A4AD-7EA5C011ABE5" />

        <!-- MobilityRedirect -->
        <Feature ID="F41CC668-37E5-4743-B4A8-74D1DB3FD8A4" />

        <!--  Site  Publish features -->
        <Feature ID="94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb" />

      </WebFeatures>
    </Configuration>
  </Configurations>
  <Modules>

    <Module Name="DefaultBlank" Url="">
      <File Url="default.aspx">
         <View List="$Resources:core,shareddocuments_Folder;" BaseViewID="1" WebPartZoneID="Left" />
      </File>
    </Module>
</Modules>
</Project>

Once the site is created its an empty publishing page. There is no webpart added. When I remove the publising features from the onet.xml and create a new site I do get the webparts, but its not a publishing page

share|improve this question
add comment

1 Answer 1

I know of 2 ways that you can add a web part to a page layout. There may be other and more efficent and correct ways. But these are the ones I have found. Hopefully they will point you in the right direction.

  • Statically adding them to the page layout. This method prevents access to the edit controls when you edit the page. If can only be modified in the VS project.
  • Declaratively add it to the elements.xml for the page layout. This will allow the web part to be modified. But has the following problems.
    • When the project is deployed a new instance of the web part is added the web part zone. The old instances of the web part need to be deleted using a FeatureActivated Event receiver
    • If changes are made the the web part in VS. These changes will only apply to new instances of the page layout.

The code for option 2 is below. This code may need to be tested and tweaked and settings changed to meet your project.

On the page layout file create your web part zone

<WebPartPages:WebPartZone ID="SearchWebPartZone" runat="server" FrameType="TitleBarOnly" />

In the elements add the web part declaratively AllUsersWebPart element. Make sure you wrap up the web part declaration with CDATA.

<?xml version="1.0" encoding="utf-8"?>
  <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Module Name="PageLayouts" Url="_catalogs/masterpage">
      <File Path="PageLayouts\yourPageLayout.aspx" Url="PageLayouts/yourPageLayout.aspx" Type="GhostableInLibrary">
        <Property Name="Title" Value="Page Layout" />
        <Property Name="MasterPageDescription" Value="page Layout" />
        <Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;" />
        <Property Name="PublishingAssociatedContentType" Value=";#Page Layout Content type;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF39000a866239b6e549198be805856fbb8101000ec0ecc41784476280021f6e9105c089" />
        <AllUsersWebPart WebPartZoneID="SearchWebPartZone" WebPartOrder="0">
          <![CDATA[
            <WebPart xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/WebPart/v2">
            <Title>Search Box</Title>
            <Description>Displays a search box that allows users to search for information.</Description>
            <FrameType>None</FrameType>
            <AllowMinimize>true</AllowMinimize>
            <AllowRemove>true</AllowRemove>
            <IsVisible>true</IsVisible>
            <GoImageUrl xmlns="urn:schemas-microsoft-com:SearchBoxEx">/_layouts/images/gosearch15.png</GoImageUrl>
            <GoImageUrlRTL xmlns="urn:schemas-microsoft-com:SearchBoxEx">/_layouts/images/gosearchrtl15.png</GoImageUrlRTL>
            <GoImageActiveUrl xmlns="urn:schemas-microsoft-com:SearchBoxEx">/_layouts/images/gosearchhover15.png</GoImageActiveUrl>
            <GoImageActiveUrlRTL xmlns="urn:schemas-microsoft-com:SearchBoxEx">/_layouts/images/gosearchrtlhover15.png</GoImageActiveUrlRTL>
            <DropDownModeEx xmlns="urn:schemas-microsoft-com:SearchBoxEx">HideScopeDD_DefaultContextual</DropDownModeEx>
            <Assembly>Microsoft.Office.Server.Search, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
            <TypeName>Microsoft.SharePoint.Portal.WebControls.SearchBoxEx</TypeName>
          </WebPart>       
        ]]>
      </AllUsersWebPart>
    </File>
  </Module>
</Elements>

A sample of the code for the Event receiver

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        using (SPSite site = properties.Feature.Parent as SPSite)
        {
            SPList list = site.GetCatalog(SPListTemplateType.MasterPageCatalog);
            SPListItemCollection items = list.Items;

            // find the right Page Layout
            foreach (SPListItem item in items)
            {
                if (item.Name.Equals("yourPageLayout.aspx", StringComparison.CurrentCultureIgnoreCase))
                {
                    SPFile file = item.File;
                    file.CheckOut();
                    try
                    {
                        // get the Web Part Manager for the Page Layout
                        SPLimitedWebPartManager wpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared);

                        // iterate through all Web Parts and remove duplicates
                        while (wpm.WebParts.Count > 1)
                        {
                            System.Web.UI.WebControls.WebParts.WebPart wp = wpm.WebParts[0];
                            wpm.DeleteWebPart(wp);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        file.Update();

                        if (file.Level == SPFileLevel.Checkout)
                        {
                            file.CheckIn("Feature Activated Event Receiver to remove duplicate Webparts");

                            if (list.EnableMinorVersions)
                            {
                                file.Publish("Feature Activated Event Receiver Published");
                            }

                            if (list.EnableModeration)
                            {
                                file.Approve("Feature Activated Event Receiver Approve");
                            }
                        }
                    }

                }
            }
        }
    }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.