通过Silverlight Not Working使用客户端对象模型将项添加到Sharepoint列表

时间:2011-09-06 01:20:10

标签: silverlight sharepoint-2010

我花了很多时间通过以下标准代码访问Silverlight访问Sharepoint 2010问题,但是onQueryFailed方法总是被命中。请帮忙,谢谢!

        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");
    }

4 个答案:

答案 0 :(得分:1)

'onQueryFailed'处理程序的签名错误。您需要将'ClientRequestFailedEventArgs'用作第二个参数:

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

如果这不是问题,您至少会看到真正的错误。

答案 1 :(得分:0)

您没有为新项目指定标题,默认情况下不能为空。尝试添加:

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

答案 2 :(得分: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));
}

答案 3 :(得分: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";