2013-10-01 25 views
0

請大家幫忙,C#中添加字段(列值)上傳的文件(文件)不工作

我試圖將字段添加到一個「剛剛上傳」文件。 將文件上載到SPFolder對象。 我想從庫或文件夾中將 文件上傳到文件的所有字段自動添加。

月1日:我從庫(SPList)的項目 從 「事件ItemAdded」 屬性:

SPList currentList = properties.List; 

第二:我得到的所有字段的字段集合從currentList:

SPFieldCollection currentListFieldItems = currentList.Fields; 

第三:現在我想每一個字段添加到CURRENTITEM(這是剛剛 上傳的文件):

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; 


        if (currentItem.Fields.ContainsField(FeldInternalName)) 
        { 
        // The Field already exists 
        } 
        else 
        { 
        // The Field is not existing, will be added 
         currentItem.Fields.Add(NeuesFeld); 
        } 

        } 

currentitem.update(); 

它不工作,因爲它總是說,所有的字段已經存在! 你能幫我嗎,我錯了什麼?

斯特芬

回答

1

不能字段添加到一個SPListItem。 ListItem已經具有您上傳文件的列表中存在的所有字段。 相反,如果你想設置您可以通過使用字段的內部名稱做一個字段的值:

currentItem["InternalNameOfField"] = "I am the new value"; 

更多信息和例子可以MSDN

+0

你好找到,謝謝大家了響應,但是當我嘗試這樣做時,出現錯誤: 屬性或索引器'Microsoft.SharePoint.SPFieldCollection.this [string]'無法分配給 - 它是隻讀的 –

+0

我的錯,您必須使用索引器的listitem,而不是字段集合。更新了我的答案並添加了一個MSDN鏈接,您可以在其中找到更多信息和示例 – roqz

+0

謝謝,我試過了: currentItem [「InternalNameofField」] =「New Value」; –