Thursday, September 25, 2014

Using Knockout and CSOM (Client Side Object Model) for pages libraries and binding the output in a jcarousel Slider - The full Example

KnockoutJS is a fantastic library when you are looking for a drop-in enhancement that brings in client side data-binding and applies the Model-View-View-Model design pattern to your websites.
Knockout works with a ViewModel that can either be a JS object or a JS function. Either ways, your ViewModel is your data source. Using Knockout, you can bind DOM elements to your model using a declarative syntax. Knockout also provides Templating (for repetitive structures in a web page) and dependency tracking using ko.observableArray. With dependency tracking, if a property is changed, it will automatically notify the UI. The UI reflects these changes and can also change the value to automatically update the source object again.
Using REST APIs and CSOM in your SharePoint implementations nowadays is a must for the rise of using SharePoint apps and SharePoint online, the glory of Server Side Object Model is fading and the technology now is going towards the  client side operations.

In this article we will discuss a simple feature - Getting SharePoint List Item form Page Library using CSOM - Then we will bind the returned results to a predefined HTML DOM elements to have those results in jcarousel slider.



  1. Get Page List Items  using CSOM
If you want to get pages (List Items) from SharePoint using Client Side scripts you have two approaches, CSOM or using REST APIs, In our case we will use CSOM as when I tried calling REST API for my pages library including the Roll up Image Field "http://server/en/News/_api/web/Lists/getbytitle('Pages')/items?$select=Id,Title,FileRef,PublishingRollupImage" , I had the following error:


Anyway to select the Pages Items we will use the below method "SelectNewsPages" I've commented the code inline with the explanation for each line :

Add the following Script links:


1
2
    <script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
    <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>

Select News Pages JS:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function SelectNewsPages() {
    //Get pages library sub web 
    var WebUrl = _spPageContextInfo.webAbsoluteUrl + "/News";
    //Load Context according to news sub site
    var context = new SP.ClientContext(WebUrl);
    var NewsWeb = context.get_web();
    //Get Pages Library (List)
    var PagesList = NewsWeb.get_lists().getByTitle('Pages');

    //Build the selection query
    //in this example we select a specific content type, Ordered By Article date desc and row limited to 5 Items

    var NewsContentTypeId = '0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D00C2CE4371BE4442CB9AE069A5FDF4163A';
    var query = new SP.CamlQuery();
    query.set_viewXml("<View><Query><Where><BeginsWith><FieldRef Name='ContentTypeId' /><Value Type='ContentTypeId'>" + NewsContentTypeId + "</Value></BeginsWith></Where><OrderBy><FieldRef Name='ArticleStartDate' Ascending='FALSE' /></OrderBy></Query><RowLimit>5</RowLimit></View>");

    //Get List Items
    var collListItem = PagesList.getItems(query);
    //Include the view fields
    context.load(collListItem, 'Include(FileRef,Title,RoutingRuleDescription,ArticleStartDate,PublishingRollupImage,ContentType)');

    //Execute your Query Async, and define the success and failure handlers
    context.executeQueryAsync(
        Function.createDelegate(this, function () {
            //DO Some Logic for Success
        }),
        Function.createDelegate(this, function () {
            alert(args.get_message());
        }));
}

Now lets under stand the Knockout view model, Simply in our model we will define:

 - The Object that we will bind ( If you are using REST APIs the data already is in JSON format so no need to define your own structure )
 - The observable array that will contain all objects to be bind
 - The get method that will fill the array

First download the following files and add the following references: 


1
2
<script type="text/javascript" src="/_layouts/15/myScripts/knockout-3.2.0.js"></script>
<script type="text/javascript" src="/_layouts/15/myScripts/ko.sp-1.0.min.Ex.js"></script>


REST API Example:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
function EmployeeModal() {
    var self = this;
    //Data array holding objects to be bind
    self.Employees = ko.observableArray([]);

    //Get method
    $.getJSON(_spPageContextInfo.webAbsoluteUrl + "/_vti_bin/listdata.svc/Employees?$expand=Skills,ModifiedBy",
                 function (data) {
                     if (data.d.results) {
                         self.Employees(ko.toJS(data.d.results));
                     }
                 }
           );
}
$(document).ready(function () {
    ko.applyBindings(new EmployeeModal());
});

For our example using CSOM :

Add following in the header


1
2
3
4
5
6
7
8
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="/_layouts/15/myScripts/jcarousel.basic.css" />
<script src="/_layouts/15/myScripts/jcarousel.basic.js" type="text/javascript"></script>
<script src="/_layouts/15/myScripts/jquery.jcarousel.min.js" type="text/javascript"></script>
<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/myScripts/knockout-3.2.0.js"></script>
<script type="text/javascript" src="/_layouts/15/myScripts/ko.sp-1.0.min.Ex.js"></script>

Following the HTML DOM to be bind

- Item Template Definition in the header


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    <%--Set the ID of the item template to be linked in the main foreach binder--%>
    <script type="text/html" id="NewsItem">
        <li>
            <a data-bind="attr: { href: Url, title: Title }">
                <img data-bind="attr: { src: Image, alt: Title }" width='900' height='395' />
            </a>
            <h3 data-bind="text: Title"></h3>
            <p data-bind="text: Summary"></p>
        </li>
    </script>

- Main Carousel container 


1
2
3
4
5
6
7
8
 <div class="jcarousel-wrapper">
        <div class="jcarousel">
            <%--The binder will itterate using foreach in the KO observable array 'sliderAllAnnouncments' , Binding the objects using the predefined item template--%>
            <ul class="jcarousel-ul" data-bind="template: { name: 'NewsItem', foreach: sliderAllAnnouncments }" />
        </div>
        <a href="#" class="jcarousel-control-prev bg-arrow-left"></a>
        <a href="#" class="jcarousel-control-next bg-arrow-right"></a>
    </div>

The Knockout View Model using CSOM - FULL CODE INCLUDING PREVIOUS SNIPPET :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//Load a loading image till the items are acquired 
$(window).load(function () {
    if ($(".jcarousel-ul").html() != null) {
        $(".jcarousel-ul").html("<CENTER><div class='loader' style='position:absolute;left:50%; top:50%;'><CENTER><img src='/_layouts/15/EQNewsScripts//ajax-loader.gif' /></CENTER></div></CENTER>");
        $(".loader").show();
        //Delay the calling till the sp.js is loaded
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', LoadNews)
    }
})

//Intialize the view model and apply bindings
function LoadNews() {
    var VM = new viewModel();
    VM.sliderRetrieveAnnouncments();
    ko.applyBindings(VM);
}

function viewModel() {
    var self = this;

    // Definition for the bind object
    self.sliderAnnouncement = function (Url, Title, Image, Summary, Date) {
        this.Url = Url;
        this.Title = Title;
        this.Image = Image;
        this.Summary = Summary;
        this.Date = Date;
    }

    // Definition for Array holding objects to be binded
    self.sliderAllAnnouncments = ko.observableArray([]);

    //Get Items using CSOM
    self.sliderRetrieveAnnouncments = function () {
        var WebUrl = _spPageContextInfo.webAbsoluteUrl + "/News";
        var context = new SP.ClientContext(WebUrl);
        var NewsWeb = context.get_web();
        var PagesList = NewsWeb.get_lists().getByTitle('Pages');
        var query = new SP.CamlQuery();
        query.set_viewXml("<View><Query><Where><BeginsWith><FieldRef Name='ContentTypeId' /><Value Type='ContentTypeId'>0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D00C2CE4371BE4442CB9AE069A5FDF4163A</Value></BeginsWith></Where><OrderBy><FieldRef Name='ArticleStartDate' Ascending='FALSE' /></OrderBy></Query><RowLimit>5</RowLimit></View>");
        var collListItem = PagesList.getItems(query);
        context.load(collListItem, 'Include(FileRef,Title,RoutingRuleDescription,ArticleStartDate,PublishingRollupImage,ContentType)');
        context.executeQueryAsync(
            Function.createDelegate(this, function () {

                //The data loaded successfully, So hide the loader section and begin pushing data
                $(".loader").hide();
                $(".jcarousel-ul").empty();

                var listItemEnumerator = collListItem.getEnumerator();
                var length = collListItem.get_count();
                if (length > 0) {
                    //Fetch the returned data and wrap to the observable array
                    while (listItemEnumerator.moveNext()) {
                        var oListItem = listItemEnumerator.get_current();

                        if (oListItem.get_item('PublishingRollupImage') != null) {
                            var URL = oListItem.get_item('FileRef');
                            var pageTitle = oListItem.get_item('Title');
                            var pageSummary = oListItem.get_item('RoutingRuleDescription');
                            var pageDate = oListItem.get_item('ArticleStartDate');

                            var pageImage = '';
                            pageImage = oListItem.get_item('PublishingRollupImage');
                            pageImage = pageImage.substr(pageImage.indexOf('src="') + 5, pageImage.length);
                            pageImage = pageImage.substr(0, pageImage.indexOf('"'));

                            //Push the new data to the data observable array 
                            self.sliderAllAnnouncments.push(new self.sliderAnnouncement(URL, pageTitle, pageImage, pageSummary, pageDate));
                        }
                    }


                    //Data are binded to the HTML DOM, Rejester the <ul> tag for jcarousel
                    $('.jcarousel')
                            .jcarousel({
                                wrap: 'circular'
                            })
                            .jcarouselAutoscroll({
                                interval: 3000,
                                target: '+=1',
                                autostart: true
                            })
                    ;
                }
            }),
            Function.createDelegate(this, function () {
                $(".loader").hide();
                alert(args.get_message());
            }));
    }
}

Here is the final result :

Happy SharePointing hope this article helps you.

SharePoint Online [Office 365] - Enable Anonymous users to Add items to list

I was recently implementing on SharePoint Online, which is a very good escape for content and publishing portals and Internet facing sites for Small & Medium enterprises which cannot afford the SharePoint Server licensing, with two alternative plans starting from 4$/month which is affordable for such business types.

We’ll have a series of articles of some challenges faced during the implementation to spread this knowledge, Specially that the term SaaS “Software as a Service” is trending and Microsoft is pushing towards Apps, cloud and for sure devices J.

The first Site Collections you have after registering for an Office 365 plan in SharePoint, SharePoint Public Site [Publishing Site], Team Site and search center with so many and many service applications configured and ready to go.

Mostly we will have interest for the SharePoint Public Site, as this is the site type that you can go online with, and enable anonymous users to access it, But in SharePoint online enabling anonymous access is just a button “Make this site online” J, You do not have the ability to set the anonymous access policy as you can do on SharePoint Server version, Even you cannot grant [Add Items], Even if you tried using SPO PS “SharePoint Online Power Shell”.

So a simple task like “Contact Us” form, creating a list with a form enabling users to add some data to the list would look like it is impossible. Then here comes the power of Apps J.

In SharePoint online the first thing you do before implementing any customization, have this Question “Is there an App for that??!” for most common tasks you will find an app for what you want to implement, Do not invest effort even time reinventing the wheel J.

Let’s get back on rails, What if the data we want to collect is a custom data, let’s get introduced to “Office 365 Anonymous Access / SharePoint 2013 Sandbox Solution” it is a Sandbox solution to manage anonymous access on Office 365 / SharePoint 2013.

OK. This is simple, Get the wsp, upload it to solution gallery then ……….. No , There is no solution gallery link under Site Settings J, Microsoft is hiding the link , But they did not prevent access to the page , So navigate to your Gallery by “ https://yourOffice365Domain-public.sharepoint.com/_catalogs/solutions/Forms/AllItems.aspx “ and yup you will be able to Upload the solution and activate it.

Note:  By default you will find this message “Your resource quota is 0 server resources. Solutions can consume resources and may be temporarily disabled if your resource usage exceeds your quota. “ message, So you need to allocate some server resources to the Public Site to be able to execute your packages:
  1. Go to the SharePoint Administration Panel
  2. Then select your public site collection, and select Server Resource Quota.
  3. Then assign some quota to the site collection
After activating the sandboxed solution, Navigate to your list and in the List tab, click on Anonymous Access

Then select Allow anonymous users to add items to this list

Now you can develop a form using SharePoint Apps, or Sandboxed solution to add the data to the list and anonymous users will be able to submit the data.

Note: I’ve tried to Using ECMA script for the form but it was not working with anonymous users, after having a call with Microsoft Support Engineer I was informed that is not allowed by Design !! , I am still investigating for it J



Thursday, April 24, 2014

Enable or disable custom ribbon button in SharePoint 2013 based on List Item Property [ Field Value ]

You would come to some scenarios in SharePoint where you want to create a ribbon custom action to perform some custom tasks to meet your business needs.

Creating a custom action in SharePoint 2013 is not different than SharePoint 2010, and there are a lot of articles explaining how to create a custom action.

Examples:

  1. SharePoint 2010 Custom Ribbon Button
What about disabling this button when a specific condition is true, The most common example you will find is disabling the ribbon button while more than on list item is selected.
  1. CommandUIHandler Element
  2. Enable or disable custom ribbon button in SharePoint 2010
But what about disabling the custom action based on a field value in the currently selected list item, For example in the Check-in & Check-out buttons in the ribbon is disabled/Enabled based on the Document [Item] Status.

Ok, Then how would you apply the same idea based on your own custom field.

Here comes the magic of using CSOM and ECMA scripts to communicate asynchronously with current list, Getting the current list item fields, then deciding based on the field value if you will Enable/Disable the button.

In the Custom action elements.xml definition you will find a section with the following tag "CommandUIHandler", This tag has "EnabledScript" attribute, Where you can right javascript to return true if enabled, False if the button is disabled.

First you need to check if only one item is selected:


function singleStatusEnable() {
    try{
        var selecteditems = SP.ListOperation.Selection.getSelectedItems();
        var ci = CountDictionary(selecteditems);

        if (ci == 1) {
            return CheckStatus(selecteditems);
        }
        else {
            return false;
        }
    }
    catch (ex) {
        alert('Error occurred: ' + ex.message);
        return false;
    }
}


Then we will have the following plan, We will create a global window variable of array type, To maintain the values of the EnabledScript.

We will use the array index as the ItemID and the value will be either true or false, Why we will do this ?? Simply to not have to check the Item Field value each type the user check/uncheck the item, as each time the item is checked or unchecked the method "RefreshCommandUI()" is called which re-validates all the ribbon buttons to decide wither to enable or disable them according to current selected Item.

If the global window variable is not defined we will initialize it - This will happen only with first selected item - Then we will check if the current item ID already exists in our array if yes we will return the value if not we will check the value asynchronously, after we get the response back from the server we will call the "RefreshCommandUI()" method to re-validate the ribbon buttons


function CheckStatus(selectedItems) {
    //Get Current Context
    var clientContext = SP.ClientContext.get_current();
    //Get Current List
    var currentList = clientContext.get_web().get_lists().getById(SP.ListOperation.Selection.getSelectedList());
    //Get Selected List Item
    var ItemId = selectedItems[0].id;

    //Check if the window global array variable was initialized or not 
    if(window.FolderStatusValue === undefined) {
        window.FolderStatusValue = new Array();
    }
    
    //Check if the current selected ID was previously saved if not Get the Item status and refresh the UI
    if (window.FolderStatusValue[ItemId] === undefined) {
        singleItem = currentList.getItemById(ItemId);
        clientContext.load(singleItem);
        clientContext.executeQueryAsync(Function.createDelegate(this, OnSucceeded), Function.createDelegate(this, OnFailed));
        return false;
    }
    
    //Return the saved value
    return window.FolderStatusValue[ItemId];
}

//When the Async request is completed save the Item value in the array and re-call RefreshCom//mandUI() method
function OnSucceeded() {
    
    var selecteditems = SP.ListOperation.Selection.getSelectedItems();
    var ItemId = selecteditems[0].id;

    var ItemStatus = singleItem.get_item('YOUR-CUSTOM-COLUMN-STATIC-NAME');
    
    
    if (ItemStatus) {
        window.FolderStatusValue[ItemId] = true; //Enable Ribbon button
        RefreshCommandUI();
    }
    else {
        window.FolderStatusValue[ItemId] = false; //Disable Ribbon button
    }
}


function OnFailed(sender, args) {
    alert('Error occurred: ' + args.get_message());
    return false;
}


Here is the full XML definition for the Custom Action  :


<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="8d2b47c5-9e1a-4ff2-9a90-071632a0e9db.ShareFolderExternal"
                RegistrationType="ContentType"
                RegistrationId="0x0120001D4A61CCFCF04620B4F487A48EABBD52"
                Location="CommandUI.Ribbon"
                Rights="AddListItems,DeleteListItems,EditListItems">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition
         Location="Ribbon.Documents.Share.Controls._children">
          <Button
           Id="8d2b47c5-9e1a-4ff2-9a90-071632a0e9db.ShareFolderExternal.Button"
           Command="ShareFolderExternally"
           Image16by16="/_layouts/15/images/Share16x16.png"
           Image32by32="/_layouts/15/images/Share32x32.png"
           LabelText="$Resources:DocumentSharing,ShareFolderCA;"
           TemplateAlias="o1"
           Sequence="11" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler   Command="ShareFolderExternally"
                            CommandAction="Javascript:
                                            function Operation(dialogResult, returnValue)
                                            {
                                              SP.UI.Notify.addNotification('Successfully Done!');

                                              SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
                                            }
                
                                            var webURL = _spPageContextInfo.webServerRelativeUrl;
                                            var selecteditems = SP.ListOperation.Selection.getSelectedItems();
                                            
                                            var ItemId = selecteditems[0].id;
                                            
                                            var options = {
                                                            url: webURL + '/_layouts/15/Progress.aspx?FolderId=' + ItemId + '&amp;ListId={ListId}',
                                                            title: 'Share Folder Externally',
                                                            allowMaximize: false,
                                                            showClose: true,
                                                            width: 400,
                                                            height: 100,
                                                            dialogReturnValueCallback: Operation
                                                          };
                                            SP.UI.ModalDialog.showModalDialog(options);"
                              EnabledScript="javascript:
                              
var singleItem;

function singleStatusEnable() {
    try{
        var selecteditems = SP.ListOperation.Selection.getSelectedItems();
        var ci = CountDictionary(selecteditems);

        if (ci == 1) {
            return CheckStatus(selecteditems);
        }
        else {
            return false;
        }
    }
    catch (ex) {
        alert('Error occurred: ' + ex.message);
        return false;
    }
}

function CheckStatus(selectedItems) {
    var clientContext = SP.ClientContext.get_current();
    var currentList = clientContext.get_web().get_lists().getById(SP.ListOperation.Selection.getSelectedList());

    var ItemId = selectedItems[0].id;

    if(window.FolderStatusValue === undefined) {
        window.FolderStatusValue = new Array();
    }
    
    if (window.FolderStatusValue[ItemId] === undefined) {
        singleItem = currentList.getItemById(ItemId);
        clientContext.load(singleItem);
        clientContext.executeQueryAsync(Function.createDelegate(this, OnSucceeded), Function.createDelegate(this, OnFailed));
        return false;
    }
    return window.FolderStatusValue[ItemId];
}

function OnSucceeded() {
    
    var selecteditems = SP.ListOperation.Selection.getSelectedItems();
    var ItemId = selecteditems[0].id;

    var ItemStatus = singleItem.get_item('YOUR-CUSTOM-COLUMN-STATIC-NAME');
    
    
    if (ItemStatus) {
        window.FolderStatusValue[ItemId] = true;
        RefreshCommandUI();
    }
    else {
        window.FolderStatusValue[ItemId] = false;
    }
}


function OnFailed(sender, args) {
    alert('Error occurred: ' + args.get_message());
    return false;
}

singleStatusEnable();
" />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
</Elements>


Hope you find this article useful, Happy SharePointing :)

Sunday, April 20, 2014

SharePoint 2013 - Document Library and Custom Field Type

We created a very simple custom field control to filter some custom data from SQL database, and the code behind is only the necessary constructors and one overload. We created a custom content type that inherits from Folder, and associate our site column/field type to the new content type. We then add that content type to a Document Library, and when we attempt to create the new 'folder' (our content type), it has only two columns: Name and My Custom Field. When we try to save it, Name blanks out and doesn't give us any information, validation error or exception. It just won't allow us to save. Weird !!!!

After some googling and search I've found that great blog Custom field types and rendering templates correlation with the new “Server Render” property of the ListFormWebPart .

In this article you will find that the problem is that my custom field type is using Server Rendering template, While SharePoint 2013 document library ListFormWebPart is using CSR [Client-Side Rendering] .

Then you have a solution, by following the Sridhar's article by editing the New & Edit form for your library changing the ListFormWebPart , find the “CSR Render Mode” option under “Miscellaneous” section.  Just choose “Server Render (ServerRender)” for the “CSR Render Mode” option .

But what about automating this process, In  my scenario I had a SharePoint Project including the field type, content type, Document library Template and List definition. All connected together

I had a web scoped  feature to provision this document library on containing this Field type.

So what I've to do to make all this work, Is to specify  Sridhar's solution in the feature activated event receiver, The below code snippet is what you have to do to get that done:




private void ChangeSentDocumentListFormWebPart(SPList SentDocLibList)
        {
            SPDocumentLibrary SentDocLib = (SPDocumentLibrary)SentDocLibList;
 
                        // Update forms
            foreach (SPForm spForm in SentDocLib.Forms)
            {
                if (spForm.Url.Contains("DispForm.aspx") || spForm.Url.Contains("EditForm.aspx") || spForm.Url.Contains("Upload.aspx"))
                {
                    string fileURL = SentDocLib.ParentWeb.Url + "/" + spForm.Url;
                    SPFile page = SentDocLib.ParentWeb.GetFile(fileURL);
 
                    using (SPLimitedWebPartManager lwpm = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
                    {
                        try
                        {
                            // Enable the Update
                            lwpm.Web.AllowUnsafeUpdates = true;
 
                            // Check out the file, if not checked out
                            SPFile file = lwpm.Web.GetFile(fileURL);
                            if (file.CheckOutType == SPFile.SPCheckOutType.None)
                                file.CheckOut();
 
                            // Find the ListFormWebPart and Update the Template Name Property
                            foreach (System.Web.UI.WebControls.WebParts.WebPart wp in lwpm.WebParts)
                            {
                                if (wp is Microsoft.SharePoint.WebPartPages.ListFormWebPart)
                                {
                                    Microsoft.SharePoint.WebPartPages.ListFormWebPart lfwp =
                                        (Microsoft.SharePoint.WebPartPages.ListFormWebPart)wp.WebBrowsableObject;
                                    lfwp.CSRRenderMode = CSRRenderMode.ServerRender;
                                    lwpm.SaveChanges(lfwp);
                                }
                            }
 
                            // Update the file
                            file.Update();
                            file.CheckIn("System Update");
 
                            // Disable the Unsafe Update
                            lwpm.Web.AllowUnsafeUpdates = false;
                        }
                        finally
                        {
                            if (lwpm.Web != null)
                            {
                                lwpm.Web.AllowUnsafeUpdates = false;
 
                                lwpm.Web.Dispose(); // SPLimitedWebPartManager.Web object Dispose() called manually
                            }
                        }
                    }
                }
            }
        }

Monday, January 27, 2014

SharePoint 2013 - Update Search Navigation Nodes for all Sitecollections & subwebs in your Web Application

Search navigation links represents the search result pages in SharePoint Search center. By default we have four search results pages in SharePoint search center, "Everything", "People", "Conversations", and "Videos" as shown in image below.

Those options are Shown as tabs in your search center



or as a drop down in your search box




But if you added your own result pages that are linked to specific result sources or query rules, You have to add the new pages to your search navigation settings per each webapplication

What if you have a Mulit-Site collection , Mullti-Webs Structure hierarchy … In the subweb search settings you will find that search settings have an option “Use the same results page settings as my parent”, But this will inhirit the search settings only not the search navigation links as the links are SPNavigationNode object,


So you will have to do this task manually…. Or just code it


I have written a powershell script file to update all the webs "DOWNLOAD FILE HERE", sites within your web application, In my case we have added new search results pages “Events”, “File Share” …etc…. So manipulate the URLs the titles to meet your needs

The Code:


function Update-SearchNav([string]$Identity)
{
 Write-Host -ForegroundColor Red "============================================="
 Write-Host -ForegroundColor Green "Updating Search Navigation at URL " -NoNewline;
 Write-Host -ForegroundColor Green $Identity

 $s = Get-SPSite $Identity
 $w = $s.RootWeb

 foreach ($w in $s.AllWebs) { 
  Write-Host -ForegroundColor Red "============================================="
  Write-Host -ForegroundColor Green "Updating Search Navigation at URL " -NoNewline;
  Write-Host -ForegroundColor Green $w.Url
  
  $SearchNav = $w.Navigation.SearchNav
  
  IF ($SearchNav -ne $NULL)
  {
   Write-Host -ForegroundColor Red "This Site Search Navigation Already containing values";
  }
  ELSE
  {
   Write-Host -ForegroundColor Red "Search Navigation was not found";
   
   Write-Host -ForegroundColor Green "Adding Search Navigation Everything";
   $Title = "Everything"
   $RelativeUrl = "/sites/SearchCentre/pages/results.aspx"
   $node = new-object -TypeName "Microsoft.SharePoint.Navigation.SPNavigationNode" -ArgumentList $Title, $RelativeUrl, $true
   $w.Navigation.SearchNav.AddAsLast($node)

   Write-Host -ForegroundColor Green "Adding Search Navigation Events";
   $Title = "Events"
   $RelativeUrl = "/sites/SearchCentre/Pages/events.aspx"
   $node = new-object -TypeName "Microsoft.SharePoint.Navigation.SPNavigationNode" -ArgumentList $Title, $RelativeUrl, $true
   $w.Navigation.SearchNav.AddAsLast($node)

   Write-Host -ForegroundColor Green "Adding Search Navigation People";
   $Title = "People"
   $RelativeUrl = "/sites/SearchCentre/Pages/peopleresults.aspx"
   $node = new-object -TypeName "Microsoft.SharePoint.Navigation.SPNavigationNode" -ArgumentList $Title, $RelativeUrl, $true
   $w.Navigation.SearchNav.AddAsLast($node)

   Write-Host -ForegroundColor Green "Adding Search Navigation Conversation";
   $Title = "Conversation"
   $RelativeUrl = "/sites/SearchCentre/Pages/conversationresults.aspx"
   $node = new-object -TypeName "Microsoft.SharePoint.Navigation.SPNavigationNode" -ArgumentList $Title, $RelativeUrl, $true
   $w.Navigation.SearchNav.AddAsLast($node)

   Write-Host -ForegroundColor Green "Adding Search Navigation File Share";
   $Title = "File Share"
   $RelativeUrl = "/sites/SearchCentre/Pages/FileShare.aspx"
   $node = new-object -TypeName "Microsoft.SharePoint.Navigation.SPNavigationNode" -ArgumentList $Title, $RelativeUrl, $true
   $w.Navigation.SearchNav.AddAsLast($node)

   Write-Host -ForegroundColor Green "Adding Search Navigation Videos";
   $Title = "Videos"
   $RelativeUrl = "/sites/SearchCentre/Pages/videoresults.aspx"
   $node = new-object -TypeName "Microsoft.SharePoint.Navigation.SPNavigationNode" -ArgumentList $Title, $RelativeUrl, $true
   $w.Navigation.SearchNav.AddAsLast($node)

   Write-Host -ForegroundColor Green "Adding Search Navigation This Section";
   $Title = "This Section"
   $RelativeUrl = $w.ServerRelativeUrl + "/_layouts/15/osssearchresults.aspx?u={contexturl}"
   $node = new-object -TypeName "Microsoft.SharePoint.Navigation.SPNavigationNode" -ArgumentList $Title, $RelativeUrl, $true
   $w.Navigation.SearchNav.AddAsLast($node)

  }
  Write-Host -ForegroundColor Red "============================================="
    } 
 
 $w.Dispose()
 $s.Dispose()
 Write-Host -ForegroundColor Red "============================================="
}

#TODO Add Your Web Application URL

$WebApplication = Get-SPWebApplication http://webapplicationurl


Foreach ($Sites in $WebApplication.Sites)
{ 
 Update-SearchNav($Sites.url.trim())
 Write-Host "Press any key to continue ..."

 $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

}