【问题标题】:c# sharepoint how to set a FIELD Value of SPListItem with FIELD Value from other SPListItem?c# sharepoint 如何使用来自其他 SPListItem 的 FIELD 值设置 SPListItem 的 FIELD 值?
【发布时间】:2013-10-02 12:15:47
【问题描述】:

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

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

但是如何将文件夹项中的每个字段值复制到 上传物品?

我不知道如何从“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已经存在” 我怎样才能读出字段的价值?? 我很沮丧,没有办法读出字段的值?? 请帮忙...

史蒂芬

【问题讨论】:

    标签: c# sharepoint spfield


    【解决方案1】:

    这是一个有用的案例,我总是尝试来自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;
        }
    

    【讨论】:

      【解决方案2】:

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

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

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

      然后使用

      folderItem[internalFieldName] = stringFieldValue;
      

      internalFieldName -> 这是字段的内部名称,您可以通过转到列表并按此字段排序来检查它,您将在查询字符串(url)中找到它的名称

      How to get lookup value from SpListItem

      【讨论】:

      • 你能用“IF THEN ELSE”语法多写代码吗?我不明白“?”和“:”语法.... :)
      • 字符串 stringFieldValue = string.Empty; if(sourceItem[internalFieldName] != null) { stringFieldValue = currentItem[internalFieldName].ToString()' } else { stringFieldValue = string.Empty; }
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 2012-05-04
      • 1970-01-01
      • 1970-01-01
      • 2014-08-16
      相关资源
      最近更新 更多