我最终将创建一个sharepoint日历。我想在这里创建一个eventslist但这里会出现这个奇怪的错误。
我创建一个eventslist,然后检查它是否已经创建。如果是,不是我想创建它,而是我的testLabel说它已经存在。
当我试图删除列表时,在myButton_Click
上,它给了我这个错误:
Microsoft.SharePoint.Client。ServerException: List 'CompanyCalendar'在URL为"http://server1"的站点不存在。
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"
永远不会为空。
你的ifListExists
变量永远不会为空,因为它没有在服务器上执行。
使用以下方法检查list是否存在:
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);
}