Tuesday, November 26, 2013

Enable or Disable Developer Dashboard in SharePoint 2013

To enable or disable Developer Dashboard in SharePoint 2013, use the following powershell script.

$contentService = ([Microsoft.SharePoint.Administration.SPWebService]::ContentService)
$devDashboardSettings =$contentService.DeveloperDashboardSettings
if($devDashboardSettings.DisplayLevel -eq "On){
$devDashboardSettings.DisplayLevel ="Off"
} else{
$devDashboardSettings.DisplayLevel ="On"
}
$devDashboardSettings.Update()


Monday, November 11, 2013

Replace SharePoint 2010 out-of-the-box Search box with custom control

When you create a SharePoint site using any templates, it will create a search box at the top right of the page and says ‘Search this site…’. There may be some situations you can’t go with out-of-the-box search box. This article will explain how to replace the OOTB search box with a custom control.

Create a SharePoint custom web part to replace the search box and include all your custom logics.

At the Elements.xml file of the web part include the following section.
xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/" >
<Control      Id="SmallSearchInputBox"
Sequence="23"         ControlClass="Search.WebParts.SearchAutoSuggestionTextBoxWebPart.SearchAutoSuggestionTextBoxWebPart"
              ControlAssembly="$SharePoint.Project.AssemblyFullName$">
</Control>
</Elements>

This will register your control through feature elements. You must specify the following properties;

Id: Give an Id for the custom control. The Id name of the search control is ‘SmallSearchInputBox’ (as specified in the master page).
Sequence: Define a lowest sequence number. If you define another control with same Id and with a lowest sequence number, the lowest value will be used.
ControlClass: Specify the code behind class file (fully qualified name) of the control.

You have created the search box. Next step is to replace the OOTB search box at the master page. You need to modify the master page to complete this.

Find the DelegateControl used for the search box.
<SharePoint:DelegateControl ID="DelegateControl1"
runat="server"
ControlId="SmallSearchInputBox" />
If you have specified a different name for the control, you need to give the correct Control Id instead.

Deploy the feature and check; the OOTB search box is replaced.

Thursday, October 3, 2013

Consume SharePoint web service using JavaScript in custom Web Parts

This article will explain how to consume web services using JavaScript in SharePoint Web Part.

Problem: you have developed a custom web service; you need to consume this in client side for a web part.

The following article will explain how to consume it in ASP.Net. http://msdn.microsoft.com/en-us/library/bb398998(v=vs.90).aspx It uses <asp:ScriptManager>  control to call the service reference. You can’t use this in SharePoint custom web parts because ScriptManager is already included in SharePoint. You will get a runtime error.

I use bellow steps to consume the service.
Open the ascx file of the Visual Web Part. Add <asp:ScriptManagerProxy>  control and set the service reference path and the script reference path as follows:
<asp:ScriptManagerProxy runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference path="/_vti_bin/MyServices/SuggestionService.asmx" />
       </Services>
       <Scripts>
              <asp:ScriptReference Path="/_layouts/MyServices/Suggestions.js" />
       </Scripts>
</asp:ScriptManagerProxy>

I have deployed the custom web service at the following location: /_vti_bin/MyServices/SuggestionService.asmx
And I have added required JavaScript methods at the following location: /_layouts/MyServices/Suggestions.js

I’m calling the web service method at the onkeyup event of the input control.
<input type="text" id="fname" onkeyup="GetCompletionList()">
<div id="Results"></div>
                                 
GetCompletionList()  is the JavaScript method added at the Suggestions.js file.
The result will be display at the div.
Below is the Suggestions.js file:
var myProxy;

// Initializes global and proxy default variables.
function pageLoad() {
    // Instantiate the service proxy.
    myProxy = new Search.SuggestionService(); 
    // Set the default call back functions.
    myProxy.set_defaultSucceededCallback(SucceededCallback);
    myProxy.set_defaultFailedCallback(FailedCallback);
}

// Processes at the onKeyUp and calls the service GetCompletionList method. 
function GetCompletionList() {
    var completionList = myProxy.GetCompletionList();
}

// Callback function that
// processes the service return value.
function SucceededCallback(result) {
    var RsltElem = document.getElementById("Results");
    RsltElem.innerHTML = result;
}

// Callback function invoked when a call to
// the service methods fails.
function FailedCallback(error, userContext, methodName) {
    if (error !== null) {
        var RsltElem = document.getElementById("Results");

        RsltElem.innerHTML = "An error occurred: " +
            error.get_message();
    }
}


new Search.SuggestionService()  is the name of the class where WebMethod is defined.

namespace Search
{
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [WebService(Namespace = "http://tempuri.org/")]
    [System.Web.Script.Services.ScriptService]
    public class SuggestionService : WebService
    {
[WebMethod]
        public string GetCompletionList()
        {
            return "Results";
        }
    }
}


Now on onkeyup event of the input control, the result will be displayed in the div control at the end.