Sunday 21 June 2015

Programmatically Create a Term Group if not exist in SharePoint 2013 Online Term Store Management


It is possible to programmatically create a term group  if not exist in SharePoint 2013 Online Term Store Management. The following method illustrates the steps:



public static Microsoft.SharePoint.Client.Taxonomy.TermGroup CreateTermGroupIfNotExist(ClientContext context, TermStore parent, Guid termGroupId, string termGroupName, string termGroupDescription = null)
        {
            Microsoft.SharePoint.Client.Taxonomy.TermGroup termGroup = null;
            try
            {
                context.Load(parent, ts => ts.Groups);
                termGroup = parent.GetGroup(termGroupId);
                context.Load(termGroup, ts => ts.Id);
                context.ExecuteQuery();

                if (termGroup.ServerObjectIsNull != null && termGroup.ServerObjectIsNull.Value)
                {
                    parent.CreateTermGroup(termGroupName, termGroupId, termGroupDescription);
                    context.ExecuteQuery();
                }
            }
            catch (Exception ex)
            {
               
                throw ex;
            }
            return termGroup;
        }

The above method will create a term group by the name termGroupName with the id termGroupId and description termGroupDescription.


Happy SharePointing.........:)

Tuesday 2 June 2015

Change look up field to allow multiple values by means of REST Call



It is possible change a look up field of a SharePoint list to allow multiple values by means of REST Call. The following code snippets illustrates the scenario:





    var hostweburl;
    var appweburl;

    hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));

function changeLookupFieldToAllowMultipleValues(listName, fieldName, callback) {

    var executor;
    executor = new SP.RequestExecutor(appweburl);
    executor.executeAsync({
        url: appweburl + "/_api/SP.AppContextSite(@target)/web/Lists/getbytitle('" + listName + "')/fields/getbytitle('" + fieldName + "')?@target='" + hostweburl + "'",
        method: "POST",
        body: "{ '__metadata': { 'type': 'SP.FieldLookup' }, 'AllowMultipleValues': true }",
        headers: {
            "content-type": "application/json; odata=verbose", "Accept": "application/json;odata=verbose", "X-HTTP-Method": "MERGE"
        },
        success: function (data) { callback({ Status: "Success", Message: data }); },
        error: function (error) { callback({ Status: "Error", Message: error }); }
    });

}


Test it on your own environment.


Happy SharePointing.....................:)

Create a sharepoin list in the host web for SharePoint hosted app by means of REST Call


It is possible to create a list in the host web by means of REST call from the app web for SharePoint hosted app. The following code snippets illustrates the scenario:



function createList(listName, onCreated, onError) {

    var executor;
    executor = new SP.RequestExecutor(appweburl);
    executor.executeAsync({
        url: appweburl + "/_api/SP.AppContextSite(@target)/web/Lists?@target='" + hostweburl + "'",
        method: "POST",
        body: "{ '__metadata': { 'type': 'SP.List' }, 'Hidden':true,  'AllowContentTypes': true,'BaseTemplate': 100,'Description': '" + listName + "', 'Title':'" + listName + "'}",
        headers: {
            "content-type": "application/json; odata=verbose", "Accept": "application/json;odata=verbose"
        },
        success: onCreated,
        error: onError
    });


}

Test it in your own environment.



Happy SharePointing....:)

Check if whether a field is exist in list by REST Call


We can check whether a field is existed in a sharepoint list by means of REST call. The following code illustrates the scenario:



    var hostweburl;
    var appweburl;

    hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));

function isFieldExist(listName, fieldName, callback) {

    var executor;
    executor = new SP.RequestExecutor(appweburl);
    executor.executeAsync({
        url: appweburl + "/_api/SP.AppContextSite(@target)/web/Lists/getbytitle('" + listName + "')/fields?$Filter=StaticName eq '" + fieldName + "'&@target='" + hostweburl + "'",
        method: "GET",
        headers: {
            "content-type": "application/json; odata=verbose", "Accept": "application/json;odata=verbose"
        },
        success: function (data) {
            var fields = JSON.parse(data.body).d.results;
            if (fields.length == 0) callback({ Status: false, Field: null });

            else callback({ Status: true, Field: fields[0] });
        },
        error: function (error) {
            callback({ Status: false, Field: null });
        }
    });


}