2011-09-06 34 views
0

通過以下標準代碼,我花了很多時間在Silverlight訪問Sharepoint 2010問題上,但onQueryFailed方法始終被打中。請幫忙,謝謝!通過Silverlight無法使用客戶端對象模型將項目添加到Sharepoint列表

 ClientContext clientContext = new ClientContext(siteUrl); 
     Web oWebsite = clientContext.Web; 
     ListCollection collList = oWebsite.Lists; 

     oList = clientContext.Web.Lists.GetByTitle("ClientList"); 

     ListItem oListItem = oList.AddItem(new ListItemCreationInformation()); 
     oListItem["Name"] = "John Doe"; 
     oListItem["Address"] = "123 main rd."; 
     oListItem.Update(); 

     clientContext.Load(oList,list => list.Title); 

     clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed); 
    } 

    private void onQuerySucceeded(object sender, ClientRequestSucceededEventArgs args) 
    { 
     UpdateUIMethod updateUI = DisplayInfo; 
     this.Dispatcher.BeginInvoke(updateUI); 
    } 

    private void DisplayInfo() 
    { 
     MyOutput.Text = "New item created in " + oList.Title; 
    } 

    private void onQueryFailed(object sender, ClientRequestSucceededEventArgs args) 
    { 
     MessageBox.Show("Failed"); 
    } 

回答

0

您沒有爲新項目指定標題,默認情況下它不能爲空。嘗試添加:

oListItem["Title"] = "My new item"; 
+0

感謝阿方索!但不是原因:我將「標題」更名爲「名稱」,「標題」實際上是我的「名稱」。 – Mike

1

'onQueryFailed'處理程序簽名錯誤。您需要用戶的「ClientRequestFailedEventArgs」作爲第二個參數:

// Example from msdn: 
private void onQueryFailed(object sender, ClientRequestFailedEventArgs args) 
{ 
    MessageBox.Show("Request failed. " + args.Message + "\n" + args.StackTrace); 
} 

如果這不是問題,你會看到真正的錯誤是至少的。

0

這也許是晚了,但這是我該怎麼做。

 ClientContext clientContext = new ClientContext(siteUrl); 
    Web oWebsite = clientContext.Web; 
    ListCollection collList = oWebsite.Lists; 

    var oList = clientContext.Web.Lists.GetByTitle("ClientList"); 

    ListItem oListItem = oList.AddItem(new ListItemCreationInformation()); 
    oListItem["Name"] = "John Doe"; 
    oListItem["Address"] = "123 main rd."; 
    oListItem.Update(); 

    clientContext.Load(oList); 


    clientContext.ExecuteQueryAsync(
       (s, e) => 
       Deployment.Current.Dispatcher.BeginInvoke(() => onQuerySucceeded(oListItem)), 
       (s, e) => 
       Deployment.Current.Dispatcher.BeginInvoke(() => onQueryFailed(e.Message)) 
      ); 

} 

private void onQuerySucceeded(ListItem item) 
{ 
    MyOutput.Text = "New item created in " + item["Title"]; 
} 

private void onQueryFailed(string error) 
{ 
    MessageBox.Show(string.Format("Failed {0}",error)); 
} 
0

在客戶端對象模型中,您總是必須使用內部名稱來訪問列。 當您將標題欄重命名爲「名稱」時,內部名稱仍然是「標題」。

你可以做這樣的事情:

Field titleField = calendarList.Fields.GetByInternalNameOrTitle("Name"); 
Field addressField = calendarList.Fields.GetByInternalNameOrTitle("Address"); 

clientContext.Load(titleField); 
clientContext.Load(addressField); 
clientContext.ExecuteQuery(); 
oListItem[titleField.InternalName] = "John Doe"; 
相關問題