如何使用客户端对象模型创建sharepoint日历列表项

时间:2012-01-26 15:50:34

标签: sharepoint-2010 sharepoint-list sharepoint-clientobject

我正在尝试使用客户端对象模型创建sharepoint日历事件。我可以创建项目并设置除“EndDate”列之外的所有列的列值。当我尝试设置此列时,我收到以下错误来自德语的消息翻译是“更新列表条目时使用了无效数据。您要更新的字段可能是写保护的。”如果跳过此列,则列出在没有enddate属性的Sharepoint服务器上创建的项目,但在日历视图中不可见。我可以在“datasheetview”类型的视图中看到项目,如果我从这里设置enddate,它也在日历视图中可见。(奇怪的是它是必填字段)

Server Exception Microsoft.SharePoint.Client.ServerException was unhandled.    Message=Es wurden ungültige Daten zur Aktualisierung des Listeneintrags verwendet. Das Feld, das Sie aktualisieren möchten, ist möglicherweise schreibgeschützt.   Source=Microsoft.SharePoint.Client.Runtime   ServerErrorCode=-2147024809   ServerErrorTypeName=System.ArgumentException   ServerStackTrace=""   StackTrace:  
       at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)  
    at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()  
       at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)  
       at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery()  
       at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()  
       at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()  
       at Sp_Ctx.Program.Main(String[] args) in_Ctx\Program.cs:line 129   InnerException:   

我的代码如下

using (ClientContext ctx = ClaimClientContext.GetAuthenticatedContext(targetSite)){ if (ctx != null)   {  
ctx.Load(ctx.Web); // Query for Web  

    ctx.ExecuteQuery(); // Execute  
            Console.WriteLine(ctx.Web.Title);  
            List list = ctx.Web.Lists.GetByTitle("calendarListName");  
            ctx.Load(list.Fields);  
            ctx.ExecuteQuery();  
            var newItem = list.AddItem(listItemCreationInfo);  
            newItem.Update();           
            newItem["Title"] = "myCalendar"   ..... 
            newItem["EventDate"]= DateTime.Now;  
            newItem["EndDate"]= DateTime.Now.AddMinutes(30);  
            newItem["Location"]= "Office";  
            newItem.Update();  
            ctx.ExecuteQuery();

}

1 个答案:

答案 0 :(得分:1)

答案很简单,将eventdate和enddate一起更新。原始解决方案是can be seen here