4

Right now I have a SharePoint-hosted app and and list in it, so right now the app and list are deployed in the Web host, how can I make so my list are deployed in the Host web?

UPDATE

Failed to get hos web title, Error: The field or the propertie Appbokning does not exist

Failed to Create the list. Passing in an empty or null value of parameter strTitle

$(document).ready(function () {
getHostWebProperties();
createlist();
event.preventDefault();
});

function getHostWebProperties() {
    var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    var currentcontext = new SP.ClientContext.get_current();

    var hostcontext = new SP.AppContextSite(currentcontext, hostUrl);
    var hostweb = hostcontext.get_web();
    currentcontext.load(hostweb, "Appbokning");
    // "Appbokning" is my title of my host web
    currentcontext.executeQueryAsync(
                        function () {
                            alert("The title of the host web of this app is " + hostweb.get_title());

                        },
                        function(sender, args) {
                            alert('Failed to get host web title. Error:' + args.get_message());


                        }
                    );
}



function createlist() {
    var web = context.get_web();
    // Create an announcement SharePoint list with the name that the user specifies.
    var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    var currentcontext = new SP.ClientContext.get_current();
    var hostcontext = new SP.AppContextSite(currentcontext, hostUrl);
    var hostweb = hostcontext.get_web();
    var listCreationInfo = new SP.ListCreationInformation();
    var list = web.get_lists();
    var listTitle;

    listTitle = list.getByTitle("AppbokningarList");
    // this is my listname that i have i VS, based on a calenderlist
    listCreationInfo.set_title(listTitle);
    listCreationInfo.set_templateType(SP.ListTemplateType.calendar);
    var lists = hostweb.get_lists();
    var newList = lists.add(listCreationInfo);
    context.load(newList);
    context.executeQueryAsync(onListCreationSuccess, onListCreationFail);
}

function onListCreationSuccess() {
    alert('List created successfully!');
}

function onListCreationFail(sender, args) {
    alert('Failed to create the list. ' + args.get_message());
}


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];
        }
    }
    return true;
}
3

The app needs to be given Manage Permissions on the Host Web in order to create a list in it. The context of the host web is retrieved by passing context of the app web and host url to SP.AppContextSite. Here is a code sample to create a list in Host Web.

function createlist() {
    // Create an announcement SharePoint list with the name that the user specifies.
    var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    var currentcontext = new SP.ClientContext.get_current();
    var hostcontext = new SP.AppContextSite(currentcontext, hostUrl);
    var hostweb = hostcontext.get_web();
    var listCreationInfo = new SP.ListCreationInformation();
    var listTitle = document.getElementById("createList").value;
    listCreationInfo.set_title(listTitle);
    listCreationInfo.set_templateType(SP.ListTemplateType.announcements);
    var lists = hostweb.get_lists();
    var newList = lists.add(listCreationInfo);
    context.load(newList);
    context.executeQueryAsync(onListCreationSuccess, onListCreationFail);
}

function onListCreationSuccess() {
    alert('List created successfully!');
}

function onListCreationFail(sender, args) {
    alert('Failed to create the list. ' + args.get_message());
}

See this for more info: http://www.sharepointnadeem.com/2013/12/sharepoint-2013-apps-access-data-in.html

UPDATE

Answer based on the updated question.

`currentcontext.load(hostweb, "Appbokning");` 

should be replaced with currentcontext.load(hostweb, "Title");. Moreover, if you want to create a Calendar list in host web named "AppbokningarList", simply replace

var listTitle;
listTitle = list.getByTitle("AppbokningarList");

with var listTitle = "AppbokningarList";

  • alright, how i do set permissions the apps on the host-web? Do u mean in the Appmanifest? – Batistuta Nov 24 '14 at 7:19
  • Yes AppManifest, App properties -> Permissions -> Content -> Web. – Nadeem Yousuf Nov 24 '14 at 7:21
  • Yes i have "Read" there.. – Batistuta Nov 24 '14 at 7:23
  • What should i add here ? var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl")); – Batistuta Nov 24 '14 at 7:32
  • It is a function which returns the host web url: 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]; } } } – Nadeem Yousuf Nov 24 '14 at 8:21

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.