1. 程式人生 > >複製SharePoint列表項(SPListItem)到另一個列表

複製SharePoint列表項(SPListItem)到另一個列表

從理論上講,有一個簡單到難以置信的解決辦法:SPListItem提供了一個CopyTo(destinationUrl)方法(可參考MSDN)。不幸的是,這個方法似乎用不了。至少對我的情況(一個帶附件的自定義列表)是如此。總是告訴我找不到源列表項,沒有讀取許可權,或者列表沒有釋出等等。從網上找到很多帖子,其他人也遇到同樣的問題。最好的解決方案就是自己實現。

首先設計方法的引數和返回值型別:

public static SPListItem CopyItem(SPListItem sourceItem,string destinationListName)

內容部分首先是建立目標列表項。然後把源列表項的欄位複製到目標項。

//複製sourceItem到destinationList
SPList destinationList=sourceItem.Web.Lists(destinationListName);
SPListItem targetItem=destinationList.Items.Add();
foreach(SPField f in sourceItem.Fields)
{
    if(!f.ReadOnlyField && f.InternalName!="Attachments")
    {
         targentItem[f.InternalName]=sourceItem[f.InternalName];
     }
}

程式碼中跳過了只讀欄位和附件。對於附件我們這樣處理:

//複製附件
foreach(string fileName in sourceItem.Attachments)
{
    SPFile file=sourceItem.ParentList.ParentWeb.GetFile(
                                   sourceItem.Attachments.UrlPrefix+fileName);
    byte[] imageData=file.OpenBinary();
    targetItem.Attachments.Add(fileName,imageData);
}

接下來只需提交目標項至資料庫並返回即可。

//儲存targetItem
targetItem.Update();
return targetItem;

對該方法的呼叫方法大致如下:

SPListItem approvedItem=CommonFunctions.CopyItem(item,"目標列表名");

為了方便Copy,下面列出完整的程式碼:

public static SPListItem CopyItem(SPListItem sourceItem, string destinationListName)
{
    //複製 sourceItem 到 destinationList
    SPList destinationList = sourceItem.Web.Lists[destinationListName];
    SPListItem targetItem = destinationList.Items.Add();
    foreach (SPField f in sourceItem.Fields)
    {
          if (!f.ReadOnlyField && f.InternalName != “Attachments”)
         {
               targetItem[f.InternalName] = sourceItem[f.InternalName];
         }
     }
     //複製附件
     foreach (string fileName in sourceItem.Attachments)
     {
             SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName);
             byte[] imageData = file.OpenBinary();
             targetItem.Attachments.Add(fileName, imageData);
      }
      targetItem.Update();
      return targetItem;
} 

對於需要進一步對目標項進行跟蹤的情況而言。預設SharePoint的SPListItem帶有一個_CopySource欄位,並且還有一個對應的CopySource屬性。應該是與CopyTo方法配合用的。同樣不幸的是都是隻讀的。沒法為我所用。基於這個原因Muhimbi的做法是在目標列表上新建一個自己的欄位——_M_CopySource,來實現類似的功能:

SPList sourceList = MyWorkflow.List;
SPList destinationList = MyWorkflow.Web.Lists[MyWorkflow.Parameter1 as String];
SPListItem sourceItem = MyWorkflow.Item;
 
// 首先檢查自定義的來源欄位在目標列表中是否存在
if (destinationList.Fields.ContainsField("_M_CopySource") == false)
{
      SPField newField = destinationList.Fields.CreateNewField("Text", "_M_CopySource");
      newField.Hidden = true;
      destinationList.Fields.Add(newField);
}
 
// 檢查是否存在需要更新的列表項
string camlQuery = "<Where>" +
                   "<Eq><FieldRef Name='_M_CopySource'/><Value Type='Text'>{0}</Value></Eq>" +
                   "</Where>";
camlQuery = string.Format(camlQuery, sourceItem["FileRef"]);
SPQuery query = new SPQuery();
query.Query = camlQuery;
query.RowLimit = 1;
 
// 查詢列表
SPListItemCollection items = destinationList.GetItems(query);
SPListItem newItem = null;
if (items.Count == 0)
    newItem = destinationList.Items.Add();
else
    newItem = items[0];
 
// 複製欄位
foreach(SPField field in sourceItem.Fields)
{
    if (newItem.Fields.ContainsField(field.InternalName) == true && 
        field.ReadOnlyField == false && field.InternalName != "Attachments")
    {
       newItem[field.InternalName] = sourceItem[field.InternalName];
    }
}
 
// 刪除目標項上已有的附件
for (int i = newItem.Attachments.Count; i > 0; i-- )
{
    newItem.Attachments.Delete(newItem.Attachments[i-1]);
}
 
// 複製所有的附件
foreach (string fileName in sourceItem.Attachments)
{
    SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + 
                                                          fileName);
    byte[] imageData = file.OpenBinary();
    newItem.Attachments.Add(fileName, imageData);
}
 
// 在目標項上記下複製的來源,以便將來對其進行更新
newItem["_M_CopySource"] = sourceItem["FileRef"]; 
newItem.Update();

他是把這段程式碼用在自定義工作流活動中。實現列表項的單向同步更新。如果在查詢中新增上我們自己的條件,就可以實現滿足特定條件的更新。確實很有用。

參考資料

相關推薦

複製SharePoint列表SPListItem一個列表

從理論上講,有一個簡單到難以置信的解決辦法:SPListItem提供了一個CopyTo(destinationUrl)方法(可參考MSDN)。不幸的是,這個方法似乎用不了。至少對我的情況(一個帶附件的自定義列表)是如此。總是告訴我找不到源列表項,沒有讀取許可權,或者列表沒有釋出等等。從網上找到很多帖子,其他

javascript操作兩個選擇列表有兩個列表,如何實現在一個列表通過雙擊和多選列表中內容新增到一個列表.

</script></head><body><table width='500' border='1' cellpadding='0' cellspacing='0'><tr><td width='45%' align="center">

Vue2+VueRouter2+Webpack+Axios 構建專案實戰2017重製版渲染一個列表出來

前情回顧 好,這章開始,真的得寫點東西了。 製作 header.vue 和 footer.vue 元件檔案。 不是本篇文章的重點,但是還是有比較講一下。在第三篇博文中,我們規劃了我們的專案檔案結構,當時保留了一個 components 的空資料夾。這裡,就是準備放

python 列表復制給一個列表,改值兩個列表均會改變備忘

兩種 另一個 表復制 target tails details 總結 拷貝 get http://blog.csdn.net/lc_lc2000/article/details/53135839 本意是使A = B,B為一個列表,結果在後續對A的操作中,導致B中的值也改變了

練習七:列表復制一個列表的數據復制到一個列表

div span port copy st3 bsp col 數據復制 習題 習題七:將一個列表的數據復制到另一個列表中。 1 list1 = [1,2,3,4,5,8] 2 3 list2 = list1[:] #直接賦值,使用: 淺復制 4

【筆記】浮動屬性float的應用06——浮動內聯列表所有步驟組合在一起

第1步 - 從簡單的無序列表開始 在本練習中,我們想要一個簡單的無序列表並將其轉換為水平導航欄。 我們將從一個簡單的無序列表開始。UL使用ID選擇器(id =“navigation”)設定樣式,並且兩個LI使用類選擇器(class =“left”和class =“right”)設定樣式。這裡

編碼實現將一個文字檔案圖片一個地方複製一個地方源路徑到目的路徑可以通過方法引數傳入

public class MyTest { public static void main(String[] args) { try { test(); } catch (Exception e) { e.printStackTrace(); } } publi

Vue源碼後記-vFor列表渲染2

property per share turn logs eno ext 形參 dsl 這一節爭取搞完!      回頭來看看那個render代碼,為了便於分析,做了更細致的註釋; (function() { // 這裏this指向vue對象 下面

Vue源碼後記-vFor列表渲染3

undefined ++ 源碼 blog back war 什麽 tns check   這一節肯定能完!      經過DOM字符串的AST轉化,再通過render變成vnode,最後就剩下patch到頁面上了。   render函數跑完應該是在這裏: func

zabbix 監控key

分鐘 xxxxx ldap cached zabbix用戶 written was not in cpuinfo Key 描述 返回值 參數 詳細說明 agent.hostname 返回被監控端名稱 字符串 - 返回配置文件中配置的被監控端的名稱 a

添加監控Item

add log label 字符 屬性 new 文本 ora fig 添加監控項:Configuration -- Hosts -- Items -- Create item Name :監控項的名字 Type :監控的方式,就是我們前面說的通過 zabbix-agen

自己搭建自動化巡檢系統 處理鄰居列表

telnet遠程登錄 cisco python 通過之前的三次實驗,我們已經可以初步的使用python通過telnet來操作cisco設備,接下來開始新一期的實驗實驗目的:網絡巡檢,之後將信息存儲在數據庫中本次實驗需要再次拓展新的拓撲,,且實驗環境改為ubuntu,後續的拓展將改為在linux環境

管理信息系統MIS權威期刊列表

else electron bus science urn notes end rate cat 學術長文: 信息系統領域(排名先後排序) MIS Quarterly(EBSCO) SCIE SSCI (全文,EndNotes) Information Systems

Zabbix監控——Zabbix創建模板templates及監控item

Zabbix創建模板 Zabbix創建監控項 zabbix create templ zabbix create item zabbix_agent主動式與被動式 生產環境中,有一個簡單的原則,那就是無監控不上線,監控系統開源方案中,zabbix也算不錯的選擇。由於其系統接口的開放性,使用

Asp.Net Core 2.0 目實戰2NCMVC一個基於Net Core2.0搭建的角色權限管理開發框架

ML 用戶 解密 https redis json uil AI 不足 本文目錄 1. 摘要 2. 框架介紹 3. 權限管理之多一點說明 4. 總結 1. 摘要   NCMVC角色權限管理框架是由最近練習Net Core時抽時間整理的

神州數碼標準訪問控制列表配置ACL

mit p地址 ces 要求 layer ip route 技術 font 配置 實驗要求:熟練掌握標準訪問控制列表配置方法 拓撲如下 R1 enable  進入特權模式 config  進入全局模式 hostname R1  修改名稱 interface s0/1 

搜索引擎基礎概念3—— 倒排列表

相關 整數 原因 tro tex 進行 大於 http 1-1   倒排列表     倒排列表用來記錄有哪些文檔包含了某個單詞。一般在文檔集合裏會有很多文檔包含某個單詞,每個文檔 會記錄文檔編號(DocID),單詞在這個文檔中出現的次數(TF)及單詞在文檔中哪些位置出現過等

python資料結構------列表

一、數字的處理函式 (一)int() 取整數部分,與正負號無關,舉例如下: 1 print(int(-3.6), int(-2.5), int(-1.4)) 2 print(int(3.6), int(2.5), int(1.4))執行結果如下:  -3 -2 -1 3 2 1 (二)/

Android 開發:初識ListView(列表佈局)

效果: xml: <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/li

【C語言】求Fibonacci數列的前20陣列

//求Fibonacci數列的前20項 #include "stdio.h" int main(){     int i,j;     int f[20] = {1,1};//賦值數列前兩項