Reputation: 11
I spent lots of time on the Silverlight access Sharepoint 2010 issue by the following standard code, but the onQueryFailed method is always hit. Please help, thanks!
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");
}
Upvotes: 0
Views: 7215
Reputation: 1
In Client Object Model, you always have to use the internal name to access columns. When you rename your Title-Column to "Name", the internal name still is "Title".
You can do something like this:
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";
Upvotes: 0
Reputation:
It is maybe to late but here is how I would do it.
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));
}
Upvotes: 0
Reputation: 14880
The 'onQueryFailed' handler has a wrong signature. You need to user the 'ClientRequestFailedEventArgs' as second parameter:
// Example from msdn:
private void onQueryFailed(object sender, ClientRequestFailedEventArgs args)
{
MessageBox.Show("Request failed. " + args.Message + "\n" + args.StackTrace);
}
If that is not the problem, you will see at least what the real error is.
Upvotes: 1
Reputation: 18530
You are not specifying a title for the new item which by default cannot be empty. Try adding this:
oListItem["Title"] = "My new item";
Upvotes: 0