在线Sharepoint:创建并添加;在提供商托管的应用程序(C#)中使用列表 [英] Sharepoint Online: Create & use List in Provider Hosted App (C#)

查看:207
本文介绍了在线Sharepoint:创建并添加;在提供商托管的应用程序(C#)中使用列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是SharePoint编程的新手,并且遇到以下问题:

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

我向我的Visual Studio SP项目(SampleAddInList)添加了一个列表.现在,我想在程序中获取列表以填写示例项.

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.

这是我的Visual Studio项目的屏幕快照,其中包含以下列表:

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:

似乎该应用尝试从SP测试站点获取列表,但是列表在那里不存在(如果不存在,项目应该自行创建列表,不是吗?).

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??).

当我尝试使用通过添加应用程序"在SP Web UI上创建的列表(另一个列表名称!)时,访问正常.

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.

有人可以提供线索吗?

这与我使用的上下文URL有关吗? 另请参阅我的其他问题:在线SharePoint:SPAppWebUrl和SPHostUrl

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

推荐答案

该应用似乎尝试从SP测试站点获取列表,但是 列表在那里丢失(项目应通过以下方式创建列表: 本身,如果不存在,不是吗?).

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??).

是的,因为 ListCollection.GetByTitle method 引发异常.

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();
        }
    }
}

用法

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);

这篇关于在线Sharepoint:创建并添加;在提供商托管的应用程序(C#)中使用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆