2

I have created a custom Content Type in a SharePoint App. I've had no problems making a list based off of this Content Type in the App Web.

Requirements of my project have changed and I'd like to create the list based off of this Content Type in the Host Web.

I'm not sure what approach to take to do this. I've created lists in the Host Web before. For example I have a function that makes an events list or a contacts list like so:

    //____________________________Required function for accessing the host site's info.___________________________________
function getQueryStringParameter(param) {
var params = document.URL.split("?")[1].split("&");    
for (var i = 0; i < params.length; i = i + 1) {
    var singleParam = params[i].split("=");
    if (singleParam[0] == param) {
        return singleParam[1];
}   
}
}

//____________________________Create a list if one does not already exist_________________________
//Base on this http://sharepointnadeem.blogspot.com/2013/12/sharepoint-2013-apps-access-data-in.html
function createList(listToCreate, typeOfList)
{
    // Create a SharePoint list with the name that the user specifies.
    var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    var hostContext = new SP.AppContextSite(currentContext, hostUrl);
    var hostweb = hostContext.get_web();
    var listCreationInfo = new SP.ListCreationInformation();

    listCreationInfo.set_title(listToCreate);

    if (typeOfList === "events")
    {
        listCreationInfo.set_templateType(SP.ListTemplateType.events);
    }
    else if (typeOfList === "contacts")
    {
        listCreationInfo.set_templateType(SP.ListTemplateType.contacts);
    }
    var lists = hostweb.get_lists();
    var newList = lists.add(listCreationInfo);
    currentContext.load(newList);
    currentContext.executeQueryAsync(onListCreationSuccess, onListCreationFail);     
}

function onListCreationSuccess() {
    //alert("We've created a " + "list since one doesn't exist yet." );
}

function onListCreationFail(sender, args) {
    //alert("We didn't create the list. Here's why: " + args.get_message());
}

I'm trying to figure out the modifications I need to make to this code to create lists based off of custom Content Types in the Host Web.

2

Take a look at this blog. It explains how to attach a workflow created in the app to a list in the host web. Attaching content types is quite a different proces than workflows, but it should give some ideas. Something along the lines of this (warning, my JSOM might be off, so check the syntax):

var clientContext = new SP.ClientContext.get_current();
var hostWebContext = new SP.AppContextSite(clientContext, hostweburl);

var appContentTypes = clientContext.get_web().get_contentTypes();
var listContentTypes = hostWebContext.get_web().get_lists().getByTitle("Your List").get_contentTypes();

clientContext.load(appContentTypes);
clientContext.load(listContentTypes);
clientContext.executeQueryAsync(function() {

    var contentTypeEnumerator = appContentTypes.getEnumerator();
    while(contentTypeEnumerator.moveNext()) {
        var contentType = contentTypeEnumerator.get_current();
        if(contentType.get_name() == "Your Content Type") {
            listContentTypes.addExistingContentType(contentType);
            break;
        }
    }
    clientContext.executeQueryAsync(function() {
        console.log("Content type was added");
    }, function(sender, args) {
        console.log("Error occurred: " + args.get_message());
    });
},
function(sender, args) {
    console.log("Error occurred: " + args.get_message());
});

This has not been tested, so it might not work. The content type may need to be added to the host web first, then added to the list, not sure. I hope this at least gets you started.

0

Have a look at this create-a-list-in-the-host-web by Steve Peschka

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.