Friday, 15 March 2013

Modify Search Center Settings with JavaScript Client Object Model

Although there is currently no API in the JavaScript CSOM which lets you change the search center url directly, SharePoint stores the search settings in the Property Bag of the root SPWeb object in a site collection. You can set these property bag values and your search settings should get modified.

"SRCH_ENH_FTR_URL" – Search Center URL

"SRCH_SITE_DROPDOWN_MODE" – Search Scope Drop down option

"SRCH_TRAGET_RESULTS_PAGE” – Target Search Results page

Using JavaScript Client Object Model, you can set the values like this:

For the Search Scope Drop Down Options, here is a full set of values you can set:

Do Not Show Scopes Dropdown, and default to contextual scope: "HideScopeDD_DefaultContextual"
Do Not Show Scopes Dropdown, and default to target results page: "HideScopeDD"
Show scopes Dropdown: "ShowDD"
Show, and default to ‘s’ URL parameter: "ShowDD_DefaultURL"
Show and default to contextual scope: "ShowDD_DefaultContextual"
Show, do not include contextual scopes: "ShowDD_NoContextual"
Show, do not include contextual scopes, and default to ‘s’ URL parameter: "ShowDD_NoContextual_DefaultURL"

Tuesday, 12 March 2013

SharePoint 2013: Modify Navigation Settings with JavaScript and JSOM

One of the new additions in the JavaScript Client Object Model in SharePoint 2013 is the Publishing Navigation API. Some of the other additions are the UserProfile API and the Taxonomy API which I have already posted about.

In this post, let us look at how to use the Navigation API to change the navigation settings in a SharePoint 2013 site.

The main reason I want to do these posts is that currently there is still no API Reference available on MSDN for them. So this might be helpful to people looking to achieve the same functionality.

Here are the things which you can change from this Navigation API: 

1) You can change the Navigation settings of Publishing Sites as well as Non-Publishing sites.

2) You can set the Navigation Provider of the Current Navigation (Quick Launch) as well as the Global Navigation (Top Navigation Bar)

3) You can set the Navigation Provider out of any of these three:
     a. Inherit from Parent Site
     b. Managed Navigation (Taxonomy) The navigation items will be represented using a Managed Metadata term set.
     c. Structural Navigation: Display the navigation items below the current site.

4) Since all this code is pure JavaScript, you can add it in a variety of places like Apps, WebParts, Pages, Page Layouts, Web Templates etc.

So without much further ado, here is the code to manipulate the Navigation Settings:

First, lets make sure that we have loaded all the required SharePoint JavaScript files on the page. I like to use the jQuery.getScript() for this. The scripts we will need are sp.runtime.js,  sp.js and sp.publishing.js.
Here is a simple way by which I load all the necessary files:

Now, the actual code:

1) Set the (Current/Global) Navigation to either "Inherit from Parent" or "Structural Navigation":

2) Set the (Current/Global) Navigation dependent upon a Term Set in the Term Store:

Hope you find this helpful!

Saturday, 2 March 2013

SharePoint 2013: Provision AppPart/ClientWebPart through Elements.xml

In my last post, we saw how we can add an app part to page by directly adding the app part code in our .aspx pages. Here is a link to my last post for those interested:

Now lets take a look at how we can provision the app part declaratively through Elements.xml file. First, you will have to make sure that the App is installed on the site on which you are trying to deploy the app part. Otherwise your app part will not be provisioned correctly.

Here is the code to put in your Elements.xml file:

Keep in mind the following few properties:

ProductId: Its the ProductId of your App. A unique identifier which differentiates your app. Please see my last post on how you can get the ProductId of your App.

FeatureId: The same as ProductId. On first glance, it looks like it should be the GUID of the feature present in your app. But thats not the case as far as I know.

ProductWebId: A new Random GUID

WebPartName: The file name of the WebPart found in your Client WebPart XML file. See previous post for  more details.

Custom Properties: You can even provision custom properties and set their default values from here. In the example, I have the myEnumProp as a custom property in my App Part. Here is a link on how to add custom web part properties to your app part: