简体   繁体   中英

How to retrieve Sharepoint List in Sharepoint App?

I have my app and my list in my site contents like so:

在此处输入图片说明

From my app, I'd like to read, write, and manipulate the data from TestList.

The contents of TestList are like so:

在此处输入图片说明

I've been trying to read it using this:

function setup() {

    var context = SP.ClientContext.get_current();

    var lists = context.get_web().get_lists();
    var list = lists.getByTitle('TestList');
    var listItem = list.getItemById("Title1"); // is Id the list title?

    context.load(listItem);
    context.executeQueryAsync(onLoadSuccess, onLoadFail);

    console.log(listItem);

}

function onLoadFail(sender, args) {
    alert(args.get_message());
    console.log(args.get_message());
};

As well as some other methods outlined here and here , but I'm constantly greeted by the error:

List 'TestList' does not exist at site with URL ' https://mysite.sharepoint.com/sites/developer/TestApp '.

I think it could have something to do with TestList being an app within the same directory level as TestApp, not a list within TestApp which is why I included the pictures. However, I can't figure out how to embed TestList within TestApp.

My other concern with making the list within the app would be that whenever I update and redeploy TestApp, it would wipe any new updates to TestList.

Can someone see what I'm doing wrong or offer some suggestions? Thanks in advance.

you need to use right ClientContext.

 var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl")); currentcontext = new SP.ClientContext.get_current(); hostcontext = new SP.AppContextSite(currentcontext, hostUrl); web = hostcontext.get_web(); // hostcontext instead of currentcontext var lists = web .get_lists(); var list = lists.getByTitle('TestList'); var listItem = list.getItemById(1); // Id is Id (number) context.load(listItem); context.executeQueryAsync(onLoadSuccess, onLoadFail); 

more details: http://blog.appliedis.com/2012/12/19/sharepoint-2013-apps-accessing-data-in-the-host-web-in-a-sharepoint-hosted-app/

I think you are on the right track. It appears that you want to work with a list on the host web, but your are using an appweb endpoint. I can think of two options:

  1. Create an instance of a list in your app (go to add item in Visual Studio and you can select SharePoint items and choose a list) and then that list will exist in the appweb when you deploy the app. As you said though, this will wipe out the list when you redeploy the app because it will uninstall and reinstall the app. Updating an app will not do the same thing if you are in production.
  2. Make cross-domain calls to the host web and work directly with the host web list instead. This may be the better solution depending on the nature of the app. Keep in mind that you must make sure you set permissions for your app to have permissions on the site collection in order for the calls to work, and then you must capture the host context in order to work with the host SP.Web object. I think this link will probably be helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM