Sharepoint Online: Create & use List in Provider Hosted App (C#)

走远了吗. 提交于 2019-12-11 12:09:12

问题


I'm new to SharePoint programming and have the following problem:

I added a List to my Visual Studio SP project (SampleAddInList). Now I want to get the list in my program to fill in an example item.

Here is the Screenshot of my Visual Studio project with the list:

Here my code where I try to get the list to add an item:

private void AddUserToList(string accessToken)
    {
        if (IsPostBack)
        {
            sharepointUrl = new Uri(Request.QueryString["SPHostUrl"]);
        }


        ClientContext clientContext =
                TokenHelper.GetClientContextWithAccessToken(
                    sharepointUrl.ToString(), accessToken);


        // Load the properties for the web object.
        Web web = clientContext.Web;
        clientContext.Load(web);
        clientContext.ExecuteQuery();

        // Get the current user.
        clientContext.Load(web.CurrentUser);
        clientContext.ExecuteQuery();
        currentUser = clientContext.Web.CurrentUser.Email;

        // Load the list "SampleAddInUserList"
        List userList = web.Lists.GetByTitle("SampleAddInList");
        clientContext.Load<List>(userList);
        clientContext.ExecuteQuery();
        ListItemCreationInformation info = new ListItemCreationInformation();

        Microsoft.SharePoint.Client.ListItem newItem = userList.AddItem(info);
        newItem.ParseAndSetFieldValue("Title", "Test");
        newItem.ParseAndSetFieldValue("Email", currentUser);
        newItem.Update();
        clientContext.ExecuteQuery();
    }

When I run the project, I get the following error:

List 'SampleAddInList' does not exist at site with URL 'https://xxx.sharepoint.com/sites/test'.

Here the error screenshot:

It seems that the app tries to get the list from the SP test Site, but the list is missing there (the project should create the list by itself if not exisiting, shouldn't it??).

When I try to use a list (another list name!) which i created on SP Web UI via "Add an app", the access works fine.

Can someone give a clue???

Has this something to do with the context URL I use??? see also my other question: Sharepoint Online: Difference between SPAppWebUrl & SPHostUrl


回答1:


It seems that the app tries to get the list from the SP test Site, but the list is missing there (the project should create the list by itself if not exisiting, shouldn't it??).

That's right, the error occurs since ListCollection.GetByTitle method throws exception if list does not exist.

You could consider the following extension method to get or create a List if it does not exist:

using System.Linq;
using Microsoft.SharePoint.Client;

namespace SharePoint.Client.Extensions
{
    public static class ListCollectionExtensions
    {
        public static List EnsureList(this ListCollection lists, string listTitle, ListTemplateType listTemplateType)
        {
            var ctx = lists.Context;
            var result = ctx.LoadQuery(lists.Where(l => l.Title == listTitle));
            ctx.ExecuteQuery();
            if (!result.Any())
            {
                var lci = new ListCreationInformation();
                lci.Title = listTitle;
                lci.TemplateType = (int)listTemplateType;
                var list = lists.Add(lci);
                ctx.Load(list);
                ctx.ExecuteQuery();
                return list;
            }
            return result.FirstOrDefault();
        }
    }
}

Usage

var customList = ctx.Web.Lists.EnsureList("Notes", ListTemplateType.GenericList);
var documentsLibrary = ctx.Web.Lists.EnsureList("Documents", ListTemplateType.DocumentLibrary);
var tasksList = ctx.Web.Lists.EnsureList("Tasks",ListTemplateType.TasksWithTimelineAndHierarchy);


来源:https://stackoverflow.com/questions/34787909/sharepoint-online-create-use-list-in-provider-hosted-app-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!