2

I am trying to provision a custom list using SharePoint 2013 Provider hosted app. For this, I added a "List" in VS.NET 2013, to the App project.

I am provisioning the List through declarative code. Here's the screenshot of the solution which I am working on.

App Solution Image

I assume this will provision a list to the site where ever the app gets added.

Please help me clarify whether my understanding is right or not.

3 Answers 3

3

Apps in sharepoint are created as a seperate site called "App Web". The List that you are creating will be created in "App Web". However, if you want to create a list in the "Host Web", then you will need to programme a little bit to get the "Context". Also The app has to be given permission on the Site where you want to create the list.

Please refere to this article for more info http://msdn.microsoft.com/en-us/library/office/fp179927(v=office.15).aspx

2

When you create a list instance via an app, it creates it on the app site, not on the host site. If you want to create a list on the host site when the app is added, you'd have to use REST or JSOM (or C# if you use a provider hosted app) when the app is activated.

2
  • Thanks for your quick response. May I know, how to get the App web url?
    – AbrahamJP
    Jul 7, 2014 at 11:29
  • 1
    You'll have to parse the query string of the url. If you left the {StandardTokens} token in the url for the page, you should be able to get the AppWeb url by getting the value for SPAppWebUrl parameter. If you need the host url, it's SPHostUrl. See this for tokens: msdn.microsoft.com/en-us/library/office/…
    – wjervis
    Jul 7, 2014 at 11:35
0

You need to acquire host web context and create list there.

function CreateList(title, url, templateType, hidden) {

 //Using the App Web as the client context
 clientContext = new SP.ClientContext.get_current();

 //Get the host web URL from the query string params
 //I have a function getHostWebUrl() - which is not included.
 // Some Ideas Here: http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
 var hostWebUrl = getHostWebUrl();
 //Using the hostWebContext as an AppContextSite
 hostWebContext = new SP.AppContextSite(clientContext, hostWebUrl);

 //Create List Code
 var listCreation = new SP.ListCreationInformation();
 listCreation.set_title(title);
 listCreation.set_templateType(templateType);
 listCreation.set_url(url);

 //must use the hostWebContext to get the list in that site
 var lists = hostWebContext.get_web().get_lists();
 var list = lists.add(listCreation);
 list.set_hidden(hidden);
 list.set_onQuickLaunch(false);
 list.update();

 //Always use the context of the app web to do the work or load and executing
 clientContext.load(list);
 clientContext.executeQueryAsync(function() {
     console.log("Created List : \"" + title + "\"");
 }, function(sender, args) {
     console.log("Failed to create list : " + title);
     console.log("<span style='color:red'>Reason : " + args.get_message() + "</span>");
 });  
 }

// parameters : List Name, List Url, Template Type (can be ListTemplateType or Template ID, Hidden)
 CreateList("My Test List", "Lists/MyTestList", SP.ListTemplateType.genericList, false);
 CreateList("My Test List", "Lists/MyTestList", 171, false);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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