Wednesday, December 17, 2014

Get current logged in username using REST API

$.ajax({
   url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=DisplayName",
   type: "GET",
   headers: {"accept": "application/json;odata=verbose"},
   success: function (data) {
      userName=data.d.DisplayName;
   },
   error: function (xhr) {
      alert(xhr.status + ': ' + xhr.statusText);
   }
}); 

Get current logged in username using Client-Side Object Model

ExecuteOrDelayUntilScriptLoaded(getUserName, "sp.js");
var userName='';
function getUserName(){
var clientContext = SP.ClientContext.get_current();
var website = clientContext.get_web();
currentUser = website.get_currentUser();
clientContext.load(website);
clientContext.load(currentUser);
clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);
}

function onRequestSucceeded() {
userName=currentUser.get_title();
alert(userName);
}

function onRequestFailed(sender, args) {
    alert('Error: ' + args.get_message());
}

Friday, December 12, 2014

Consuming Rest Service

            BasicHttpBinding basicHttpbinding = new BasicHttpBinding(BasicHttpSecurityMode.None);
            basicHttpbinding.Name = "BasicHttpBinding_temp";
            basicHttpbinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
            basicHttpbinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
            EndpointAddress endpointAddress = new EndpointAddress("http://siteurl/Service.svc");

Create an object for the service referenced and pass the above two objects as its constructor parameter