c# sharepoint 如何使用其他 SPListItem 的字段值设置 SPListItem 的字段值?

标签 c# sharepoint spfield

我想将多个文档(文件)上传到 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();

上面的代码不起作用,它总是给出消息“字段已存在” 我怎样才能读出字段的值? 我很沮丧没有方法读出字段的值? 请帮忙...

史蒂芬

最佳答案

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

关于c# sharepoint 如何使用其他 SPListItem 的字段值设置 SPListItem 的字段值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19136664/

相关文章:

sharepoint - SharePoint 中的多个 Web 部件和功能区

sharepoint - 要学习哪个 CodePlex/Google 代码项目以学习 SharePoint 最佳实践

sharepoint - 我可以使用 C# 在 Sharepoint 2010 中为计算字段创建自己的公式吗

c# - Visual Studio 在构建期间编译 WPF 应用程序两次

c# - C#读写文件的不同方式

c# - 从 Windows 移动应用程序获取文件的图标

c# - 从 Google 图书下载文件

javascript - Bootstrap 图像响应在 IE 和 Firefox 中不起作用,但在 Google Chrome 中工作正常

SharePoint - 数字字段是什么类型