0

I am able to create new list items programmatically with JSOM, however I can't find the correct code to set content types on the new items.

I have tried the following two methods :

oListItem["ContentTypeID"] = "Employee Folder";

oListItem.set_item('ContentTypeID', 'EmployeeFolder');

What am I missing here? Thanks in advance

4

SharePoint allows us to update the content type to the list item by specifying the content type id.

SP.ListItem.set_item('ContentTypeId','<Id of the content type>');

Please check the below snippet to update the content type to the listitem,

//This is a ContentType ID for Announcement content type
var contenttypeId = '0x01040005B1FCA568800F4FB4162C7A09742E67';
var lisTitle = "TestNewList";
var listitemId = 1;
UpdateListItem(lisTitle, listitemId, contenttypeId);

function UpdateListItem(listTitle, listitemId, contenttypeId) {
var clientContext = new SP.ClientContext();
oList = clientContext.get_web().get_lists().getByTitle(listTitle);
oListItem = oList.getItemById(listitemId);
oListItem.set_item('ContentTypeId', contenttypeId);
oListItem.update();
clientContext.load(oListItem, 'Id', 'Title', 'ContentTypeId');
clientContext.executeQueryAsync(
    Function.createDelegate(this, function() {
        var listsInfo = '';
        listsInfo += oListItem.get_item('Title') + '\t' + oListItem.get_item('ContentTypeId');
        console.log(listsInfo.toString());
    }),
    Function.createDelegate(this, function(sender, args) {
        console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }));

}

I hope, this solves your problem. :)

0

Have you created the contenttypes in the Site Collection?

I can't post all code

var web = context.get_web();
webContentTypes = web.get_contentTypes();
var contentType = new SP.ContentTypeCreationInformation();
contentType.set_group(contentTypeGroup);
contentType.set_name(contentTypeName);
contentType.set_description(contentTypeDescription);
webContentTypes.add(contentType);
context.load(webContentTypes);
context.executeQueryAsync();
  • 2
    yes they are previously created content types. When I run the calls though, I get the error "column ContentTypeID doesn't exists" – cmoody4 Feb 27 '16 at 1:12
  • Hi, the internal names are case sensitive, use "ContentTypeId" instead of "ContentTypeID". And pass the Id of the contentType as a value. This will solves your problem. – Shantha Kumar Thambidurai Feb 28 '16 at 2:19
0

Here you got it:

http://www.c-sharpcorner.com/UploadFile/4e3ce7/create-site-content-type-in-sharepoint-2013-using-jsom398/

function createContentType() {  
if (hostWebContext != undefined && hostWebContext != null) {  
    var hostWeb = hostWebContext.get_web();  
    var contentTypeCollection = hostWeb.get_contentTypes();  
    //Get Document Content type by its Id. We will use Document Content Type as Base Content Type  
    //to create a new Content Type.  
    //Refer article for Ids of other Content Types.  
    var contentType = contentTypeCollection.getById("0x0101");  
    //Creating new Content Type  
    var newContentType = new SP.ContentTypeCreationInformation();  
    newContentType.set_name('Employee');  
    newContentType.set_group('Employee Details');  
    newContentType.set_description('Content Type for Employee Details.');  
    //Set Base Content Type  
    newContentType.set_parentContentType(contentType);  
    contentTypeCollection.add(newContentType);  
    context.load(contentTypeCollection);  
    context.executeQueryAsync(  
        function () {  
            alert('Content type created successfully on Host Web.');  
        },  
       function onContenttypeFailed(sender, args) {  
           alert('Content type creation failed. Error: ' + args.get_message() + '\n' + args.get_stackTrace());  
       });  
}  

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.