c#sharepoint如何使用来自其他SPListItem的FIELD值设置SPListItem的FIELD值?

时间:2013-10-02 12:15:47

标签: c# sharepoint spfield

我想将多个文档(文件)上传到Sharepoint FOLDER。 在“ItemAdded”事件期间,我想“复制”父文件夹的FIELDS (SPListItem)到当前(已上传)的项目。

当我检查当前项目的FIELDS时,它们都是 已经在那了。

但是我如何将每个FIELD VALUE从Folder Item复制到 上传项目?

我不知道如何从“ItemSource”中读出FIELDS的值

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已存在” 我怎么能读出FIELD的值? 我很沮丧没有方法可以读出FIELD的值? 请帮忙......

斯特芬

2 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分:0)

首先,您需要从该字段中获取值。每个sharepoint字段类型,在c#中都有自己的类表示。如果你有字符串值,那很容易,但是当你有关系时,你需要使用SPFieldLookup等。

当你有字符串时,你可以这样写:

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

然后使用

folderItem[internalFieldName] = stringFieldValue;

internalFieldName - &gt;这是字段的内部名称,您可以通过转到列表并按此字段排序来检查它,并且您将在querystring(url)中将其命名为

How to get lookup value from SpListItem

相关问题