Monday, September 7, 2015

Creating Site columns, Content Type and View in Sharepoint 2013/ O365 using JSOM

1.  Creating Site Column using JSOM



var context;
var web;
var fields;

var addField = function () {
   context = new SP.ClientContext.get_current();
   web = context.get_web();
   fields = web.get_fields();
   var fieldSchema = '                              Name="ExpiryDate" \
                             DisplayName="Expiry Date" \
                             Format="DateOnly" \
                             Required="TRUE" \
                             Group="Contoso Columns" />';
   fields.addFieldAsXml(fieldSchema, false, SP.AddFieldOptions.addFieldCheckDisplayName);
   context.executeQueryAsync(onAddFieldSuccess, onAddFieldFail);
}


var onAddFieldSuccess = function () {
   alert('Field created');

}

var onAddFieldFail = function () {
   alert('Something went wrong');

}

2.  Creating Content Type and View using JSOM


function createContentType() {
    var context = new SP.ClientContext(appWebUrl);
    var appCtxSite = new SP.AppContextSite(context, hostWebUrl);
    var web = appCtxSite.get_web();
    web.contentTypeCollection = web.get_contentTypes();
    web.contentType = contentTypeCollection.getById("0x0101");
    var newContentType = new SP.ContentTypeCreationInformation();
    newContentType.set_name('Custom Jay Content Type');
    newContentType.set_description('Jay custom content type');
    newContentType.set_parentContentType(contentType);
    web.contentTypeCollection.add(newContentType);
    context.load(list.contentTypeCollection);
    context.executeQueryAsync(success, fail);
}


function createView() {
    var context = new SP.ClientContext(appWebUrl);
    var appCtxSite = new SP.AppContextSite(context, hostWebUrl);
    var web = appCtxSite.get_web();

    var listCollection = web.get_lists();
    var list = listCollection.getByTitle("CategoryList");
    list.viewCollection = list.get_views();

    var viewInfo = new SP.ViewCreationInformation();
    viewInfo.set_title('Jay View');
    list.viewCollection.add(viewInfo);

    context.load(list.viewCollection);
    context.executeQueryAsync(success, fail);

}

No comments: