Wednesday, 17 June 2015

Display Author and Last Modified Date in Item display template SharePoint 2013 search results

This post explains how you can display author and last modified in item display templates.

If you take any display template you get to see Manged Property mappings of 'EditorOWSUSER' and 'LastModifiedTime'.
These manged property holds value for user who modified the item(anything like blog, document etc) and last modified time.

Open SharePoint designer and Edit your item template html in Advanced mode.

First get the values of the both as follows
To get User:
var author = ctx.CurrentItem.EditorOWSUSER
This will get the user object data from context, which looks something like

| Ph2AdminQA | 693A30232E777C6D696E64747265655C70683261646D696E7161 i:0#.w|domain\ph2adminqa

If you want only User diplay name,$getItemValue() comes to rescue
var author =$getItemValue(ctx, "EditorOWSUSER")
This function returns the display name for the property.

To Get Date&Time:
var modifiedDate= ctx.CurrentItem.LastModifiedTime
This will get the date and time in UTC format like Sat Jul 19 2014 11:51:22 GMT+0530 (India Standard Time)

If you want only Date, use $getItemValue() as
var modifiedDate= $getItemValue(ctx, "LastModifiedTime")
This will give Time like Saturday, July 19, 2014


Now once you have both the values, you can display in any div or create your own html element and add it below the ctx.RenderBody(ctx) call.
<div> Modified by,_#= author =#_  On _#= modifiedDate=#_</div>


Refer http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2014/02/26/useful-javascript-for-working-with-sharepoint-display-templates-spc3000-spc14.aspx
for more useful tips when working with display templates

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.