2016-01-18 61 views
0

我有一個SharePoint列表,其中有100個項目,我最初從SQL中的一個表中填充。現在,我想更新那些已更改的項目並添加新項目。如何根據列表中的列更新項目?任何幫助深表感謝。SharePoint - 更新列表項如果ID存在其他AddItem

這是我已初步添加的項目:

  Microsoft.SharePoint.Client.List oList_Donors = context.Web.Lists.GetByTitle("XYZ"); 

      ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation(); 

      using (OdbcConnection connection = new OdbcConnection()) 
      { 
       connection.ConnectionString = "dsn=abc;uid=efg;pwd=hij;DataSource=klm"; 
       connection.ConnectionTimeout = 100; 
       connection.Open(); 

       OdbcCommand command_abc = new OdbcCommand("Select * From vw_SP_abc_efg", connection); 

       try 
       { 
        using (OdbcDataReader reader = command_abc.ExecuteReader()) 
        { 
         while (reader.Read()) 
         { 

          var obj0 = reader.GetValue(48); 
          var obj1 = reader.GetValue(0); 
          var obj2 = reader.GetValue(33); 
          var obj3 = reader.GetValue(47); 
          var obj4 = reader.GetValue(42); 
          var obj5 = reader.GetValue(42); 


          ListItem oListItem_abc = oList_abc.AddItem(itemCreateInfo); 


          oListItem_abc["Title"] = (obj0 == null || obj0.Equals(DBNull.Value)) ? "" : reader.GetString(48).ToString(); 
          oListItem_abc["abc_x0020_ID"] = (obj1 == null || obj1.Equals(DBNull.Value)) ? "" : reader.GetString(0).ToString(); 
          oListItem_abc["Excluded_x0020_By"] = (obj2 == null || obj2.Equals(DBNull.Value)) ? "" : reader.GetString(33).ToString(); 
          oListItem_abc["Excluded_x0020_On"] = (obj3 == null || obj3.Equals(DBNull.Value)) ? "" : reader.GetDateTime(47).ToString("MM/dd/yyyy"); 
          oListItem_abc["Reason"] = (obj4 == null || obj4.Equals(DBNull.Value)) ? "" : reader.GetString(42).Substring(50, reader.GetString(42).ToString().Length - 50); 
          oListItem_abc["Publish"] = (obj5 == null || obj5.Equals(DBNull.Value)) ? "" : reader.GetString(42).Substring(50, reader.GetString(42).ToString().Length - 50); 


          oListItem_abc.Update(); 

          context.ExecuteQuery(); 

         } 

        } 

       } 

       catch (Exception exception) 
       { 
        Console.WriteLine("The Error is:" + exception); 
        Console.ReadLine(); 
       } 

      } 
+0

您是否擁有該項目的共享點ID? –

+0

我的ID不是SharePoint ID。我也可以激活sharepoint id – SharePointDummy

+0

如果你有一個主鍵,你可以映射兩者只需做一個caml查詢來獲取項目並更新它 –

回答

0

首先,我會做一個數據庫查詢,你已經有了。之後,您將獲得DB項目的ID和所有其他信息(例如時間戳,其他字段...)。有了讀者/循環內的這個項目,我會爲每個數據庫項目創建一個caml查詢,並根據數據庫id爲您的sharepoint列表激發一個過濾器。然後你會得到一個Listitem。我不是,然後在數據庫項目列表中創建新的項目。

如果您得到一個Listitem,您可以將sharepoint項目的字段與reader對象的db字段進行比較。

如果有任何區別,您可以更新listitem。

但是,請注意,在許多商品的SharePoint列表上進行的此類操作非常昂貴(時間)且運行時間很長。