2

我最终将创建一个共享点日历​​。我想在这里创建一个事件列表,但我会在这里得到这个奇怪的错误。

我创建了一个事件列表,然后检查[查看]它是否已经创建。如果是,我不想创建它,但我的 testLabel 说它已经存在。

当我尝试删除列表时myButton_Click,它给了我这个错误:

Microsoft.SharePoint.Client.ServerException:URL 为“ http://server1 ”的站点中不存在列表“CompanyCalendar ”。

代码

using Microsoft.SharePoint.Client;

namespace CalendarWeb.Pages
{
public partial class Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

        Uri hostWeb = new Uri(Request.QueryString["SPHostUrl"]);

        using (var clientContext = TokenHelper.GetS2SClientContextWithWindowsIdentity(hostWeb, Request.LogonUserIdentity))
        {
            Web web = clientContext.Web;
            ListCreationInformation listCreator = new ListCreationInformation();
            listCreator.Title = "CompanyCalendar";
            listCreator.Description = "Workcalendar";

            listCreator.TemplateType = (int)ListTemplateType.Events;
            var ifListExcists = web.Lists.GetByTitle(listCreator.Title);
            if (ifListExcists == null)
            {
                web.Lists.Add(listCreator);
                clientContext.ExecuteQuery();
                testLabel.Text = "The " + listCreator.Title + " list was created";
            }
            else
            {
                testLabel.Text = "List already excist";
            }
        }
    }

    protected void myButton_Click(object sender, EventArgs e)
    {
        Uri hostWeb = new Uri(Request.QueryString["SPHostUrl"]);
        using (var clientContext = TokenHelper.GetS2SClientContextWithWindowsIdentity(hostWeb, Request.LogonUserIdentity))
        {
            Web web = clientContext.Web;

            var deleteList = web.Lists.GetByTitle("CompanyCalendar");
            deleteList.DeleteObject();
            clientContext.ExecuteQuery();
            testLabel.Text = "list deleted";
        }

    }
}
}

当我查看我的 server1 站点时,该列表不存在,但在代码中它似乎存在,因为我的变量"ifListExcists"永远不会为空。

4

1 回答 1

3

您的ifListExists 变量永远不会为空,因为它没有在服务器上执行。

使用以下方法检查列表是否存在:

private bool ValdiateList(ClientContext clientContext, string listName, out List existingList)    
{

    Web web = clientContext.Web;

    existingList =null;

    ListCollection lists = web.Lists;

    var existingLists 
                  = clientContext.LoadQuery(lists.Where(list => list.Title == listName));

    clientContext.ExecuteQuery();

    existingList = existingLists.FirstOrDefault();

    return (existingList != null);

}
于 2014-05-26T17:10:29.770 回答