Sunday, 16 November 2014

Adding WebPart in Custom PageLayout

When we want to deploy an OOTB webpart in a custom page layout, handy solution would be to use  <AllUsersWebPart>.

Step 1:- Configure the settings of an OOTB webpart and export it.
Step 2:- Copy the contents of the webpart file.
Step 3:- Paste in Elements file of Module as given below inside <AllUsersWebpart> by specifying the webpart zone Id and Webpart order.

In below snippet i am adding OOTB Media WebPart to custom page layout

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="PageLayouts" Url="_catalogs/masterpage" List="116">
    <File Path="PageLayouts\BlankWebPartPageCustom.aspx" Url="BlankWebPartPageCustom.aspx" Type="GhostableInLibrary" IgnoreIfAlreadyExists="TRUE" >         
        <AllUsersWebPart WebPartOrder="0" WebPartZoneID="Header">
          <![CDATA[
          <webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <type name="Microsoft.SharePoint.Publishing.WebControls.MediaWebPart, Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
      <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="Hidden" type="bool">False</property>
        <property name="AutoPlay" type="bool">False</property>
        <property name="Height" type="unit">360px</property>
        <property name="TitleUrl" type="string" />
        <property name="Width" type="unit">640px</property>
        <property name="MediaSource" type="string" null="true" />
        <property name="Loop" type="bool">False</property>
        <property name="AllowConnect" type="bool">True</property>
        <property name="HelpUrl" type="string" />
        <property name="AllowZoneChange" type="bool">True</property>
        <property name="PreviewImageSource" type="string">/sites/TestCommunity/Style Library/Media Player/VideoPreview.png</property>
        <property name="TemplateSource" type="string" null="true" />
        <property name="DisplayMode" type="Microsoft.SharePoint.Publishing.WebControls.MediaDisplayMode, Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">Inline</property>
        <property name="ExportMode" type="exportmode">All</property>
        <property name="AllowHide" type="bool">True</property>
        <property name="AllowEdit" type="bool">True</property>
        <property name="Description" type="string">Use to embed media clips (video and audio) in a web page.</property>
        <property name="IsPreviewImageSourceOverridenForVideoSet" type="bool">False</property>
        <property name="AllowClose" type="bool">True</property>
        <property name="ChromeType" type="chrometype">None</property>
        <property name="ChromeState" type="chromestate">Normal</property>
        <property name="CatalogIconImageUrl" type="string" />
        <property name="HelpMode" type="helpmode">Navigate</property>
        <property name="TitleIconImageUrl" type="string" />
        <property name="ShowEmbedControl" type="bool">False</property>
        <property name="AllowMinimize" type="bool">True</property>
        <property name="Title" type="string">Media Web Part</property>
        <property name="VideoSetEmbedCode" type="string" null="true" />
        <property name="VideoSetSource" type="string" null="true" />
        <property name="ConfigureFromContext" type="bool">False</property>
        <property name="Direction" type="direction">NotSet</property>
      </properties>
    </data>
  </webPart>
</webParts>
        ]]>
        </AllUsersWebPart>
    </File>
  </Module>
</Elements>

If multiple webparts has to be deployed just add more <File> tags with <AllUserWebPart> as child.

Wednesday, 12 November 2014

Configure SharePoint site search settings

I came across this requirement, in which i need to configure the site collection level search settings programmatically, so that searching in a site collection will be redirected to custom search page in search center.
Here is what we need to do in case of settings the url via code.

//All URLs are for testing purpose only, please change according your needs
            using (SPSite site = new SPSite("Your site url"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    web.AllProperties["SRCH_ENH_FTR_URL"] = "/sites/search/pages";                    
                   
//For site collection level
                    web.AllProperties["SRCH_SB_SET_SITE"] = "{\"Inherit\":false,\"ResultsPageAddress\":\"/sites/Search/Pages/Results.aspx\",\"ShowNavigation\":false}";                    
                    //For subsite
                    web.AllProperties["SRCH_SB_SET_WEB"] = "{\"Inherit\":false,\"ResultsPageAddress\":\"/sites/Search/Pages/Results.aspx\",\"ShowNavigation\":false}";                    
                    web.Update();
web.AllowUnsafeUpdates = false;
                }
            }

In above code replace the highlighted relative url, to search results page url of your search center.
Once done given url will set as below.