将SharePoint列表项与数据表行CSOM同步

时间:2016-12-12 06:32:30

标签: sharepoint sharepoint-2010 office365 csom

我想使用基于ticket-id的数据表数据更新我的SharePoint列表,即如果ticket-id已经在列表中我想要更新其他字段,或者在SharePoint列表中添加新项目。

我尝试过以下代码。它只添加列表项,无法更新它们:

using (ClientContext context = new ClientContext("https://example.com/list"))

        {
            SecureString passWord = new SecureString();

            foreach (char c in "xxxxxx".ToCharArray()) passWord.AppendChar(c);
            context.Credentials = new SharePointOnlineCredentials("xyz@xyz.onmicrosoft.com", passWord);
            context.Load(context.Web);
            context.ExecuteQuery();
            List oList = context.Web.Lists.GetByTitle("list");

            foreach (DataRow dr in dt.Rows) //my datatable is loaded
            {
               // Console.WriteLine(dr);
                ListItemCreationInformation listCreationInformation = new ListItemCreationInformation();
                ListItem oListItem = fusoList.AddItem(listCreationInformation);
                context.Load(oListItem);
                CamlQuery listquery = CamlQuery.CreateAllItemsQuery(1000);
                var items = oList.GetItems(listquery);
                context.Load(items);
                //context.ExecuteQuery();
                foreach (ListItem oItem in items)
                {
                    //context.Load(oItem);
                    if (oItem["Ticket_x002d_ID"] == dr["Ticket-ID"])
                    {
                        oItem["Title"] = dr["Short Description+"];
                        oItem["Assigned_x0020_To_x0020_Group"] = dr["Assigned To Group"];
                        //oItem["Ticket_x002d_ID"] = dr["Ticket-ID"];
                        oItem["Lastname"] = dr["Lastname+"];
                        oItem["Assigned_x0020_To_x0020_Individu"] = dr["Assigned To Individual"];
                        oItem["Priority"] = dr["Priority"];
                        oItem["Ticket_x002d_Type"] = dr["Ticket-Typ"];
                        oItem["Department"] = dr["Department"];
                        oItem["Status"] = dr["Status"];
                        oItem["Modification_x0020_Date"] = dr["Modification Date"];
                        oItem["StatusAttribute"] = dr["StatusAttribute"];
                        oItem["Creation_x0020_Date"] = dr["Creation Date"];
                        oItem["Firstname"] = dr["Firstname+"];
                        oItem["Remark"] = dr["Remark 2"];
                        oItem.Update();
                    }
                    else
                    {
                        oListItem["Title"] = dr["Short Description+"];
                        oListItem["Assigned_x0020_To_x0020_Group"] = dr["Assigned To Group"];
                        oListItem["Ticket_x002d_ID"] = dr["Ticket-ID"];
                        oListItem["Lastname"] = dr["Lastname+"];
                        oListItem["Assigned_x0020_To_x0020_Individu"] = dr["Assigned To Individual"];
                        oListItem["Priority"] = dr["Priority"];
                        oListItem["Ticket_x002d_Type"] = dr["Ticket-Typ"];
                        oListItem["Department"] = dr["Department"];
                        oListItem["Status"] = dr["Status"];
                        oListItem["Modification_x0020_Date"] = dr["Modification Date"];
                        oListItem["StatusAttribute"] = dr["StatusAttribute"];
                        oListItem["Creation_x0020_Date"] = dr["Creation Date"];
                        oListItem["Firstname"] = dr["Firstname+"];
                        oListItem["Remark"] = dr["Remark 2"];
                        oListItem.Update();
                    }
                }
                context.ExecuteQuery();
            }
            context.ExecuteQuery();

        }

    } 

任何人都可以帮我实现这个目标吗?

1 个答案:

答案 0 :(得分:0)

这个commans错了:

ListItem oListItem = fusoList.AddItem(listCreationInformation);

我认为你需要INSERT:

ListItem oListItem = oList.AddItem(listCreationInformation);

但仍然不确定这是否有效。