2013-10-02 68 views
2

我想將多個文檔(文件)上傳到Sharepoint文件夾中。 在「ItemAdded」事件期間,我想將父文件夾 (SPListItem)的FIELDS「複製」到當前(上傳的)項目。c#sharepoint如何設置SPListItem的FIELD值來自其他SPListItem的FIELD值?

當我檢查當前項目的FIELDS時,它們全部都是 已經存在。

但是如何將每個FIELD VALUE從文件夾項目複製到 上傳的項目?

我不知道如何從「的ItemSource」讀出字段的值

SPList currentList = properties.List; 
SPDocumentLibrary oDocumentLibrary = (SPDocumentLibrary)currentList; 
SPListItemCollection collListItems = oDocumentLibrary.Items; 
int AnzahlItems = collListItems.Count; 
SPFieldCollection currentListFieldItems = currentList.Fields; 
int AnzahlFields = currentListFieldItems.Count; 
// --------------------------------- 
// Get the current Item in the List 
// --------------------------------- 
SPListItem currentItem = currentList.Items[AnzahlItems - 1]; 
SPFieldCollection currentItemFields = currentItem.Fields; 
int currentItemFieldsAnzahl = currentItemFields.Count; 

// ----------------------------------------------------------- 
// For every FIELD from Source Item ADD FIELD to Target Item 
// ----------------------------------------------------------- 
       for (int i = 0; i < AnzahlFields; i++) 
       { 

        SPField NeuesFeld = currentListFieldItems[i]; 
        String FeldInternalName = currentListFieldItems[i].InternalName; 
        String FeldName = currentListFieldItems[i].Title; 
        NeuesFeld.Type = currentListFieldItems[i].Type; 
        NeuesFeld.Required = currentListFieldItems[i].Required; 
        NeuesFeld.ShowInEditForm = true; 
        NeuesFeld.ShowInDisplayForm = true; 
        NeuesFeld.ShowInListSettings = true; 
        NeuesFeld.ShowInNewForm = true; 
        NeuesFeld.ShowInViewForms = true; 

        // :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
        // Folder Item 1 --> Felder anhängen :::::::::::::::::::::::::::: 
        // :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 

        if (currentItem.Fields.ContainsField(FeldInternalName)) 
        { 
        // The FIELD is already existing at the Target Item 

        } 
        else 
        { 
        // The FIELD is not existing at Target Item, will be added 
         currentItem.Fields.Add(NeuesFeld); 
        } 

       } // end for 

       // ---------------------------- 
       // Save Changes at Item 
       // ---------------------------- 
       currentItem.Update(); 

這上面的代碼不工作,它總是給消息「領域已經存在」 哪有我讀出FIELD的VALUE? 我很沮喪沒有辦法讀出一個字段的值? 請幫助...

斯特芬

回答

0

首先,你需要從外地取值。每個sharepoint字段類型在c#中都有自己的類表示。當你有字符串值,這很容易,但是當你有關係,你需要使用SPFieldLookup等

當你有字符串,你可以寫這樣的事情:

string stringFieldValue = sourceItem[internalFieldName] != null ? currentItem[internalFieldName].ToString() : string.Empty; 

然後用

folderItem[internalFieldName] = stringFieldValue; 

internalFieldName - >這是字段的內部名稱,你可以進入列表和排序本場檢查它,你就會有它在查詢字符串名稱(URL)

How to get lookup value from SpListItem

+0

謝謝你,我會嘗試這個 –

+0

能否請你寫的代碼更「IF THEN ELSE」語法? 我不明白「?」和「:」語法.... :) –

+0

string stringFieldValue = string.Empty; 如果(sourceItem [internalFieldName]!= NULL){ stringFieldValue = CURRENTITEM [internalFieldName]的ToString()」 } 別的 { stringFieldValue =的String.Empty; } – zchpit

3

這是很有用的情況下,我總是嘗試從源Get And Set Value By Field Internal Name

void UpdateSPListItem(SPListItem item, Model pageItem) 
    { 
     SetValueInternalName(item, "ArticleByLine", pageItem.ArticleByLine); 

     SetValueInternalName(item, "Comments", pageItem.Comments); 

    } 
    void SetValueInternalName(SPListItem item, string fieldInternalName, string value) 
    { 
     SPField field = item.Fields.GetFieldByInternalName(fieldInternalName); 
     item[field.Id] = value; 
    } 
相關問題