2

I want to upload mulitple Documents (Files) into a Sharepoint FOLDER. During the "ItemAdded" Event I want to 'copy' the FIELDS of the Parent Folder (SPListItem) to the current (uploaded) Item.

When I check for the FIELDS of the current Item, all of them are already there.

But HOW can I copy every FIELD VALUE from the Folder Item to the uploaded Item?

I dont know how to READ OUT the Values of the FIELDS from "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();

This code above is not working, it gives always the message "The FIELD is already existing" How can I read out the VALUE of the FIELD ?? I am frustrated there is no method to read out the Value of a FIELD ?? Please help...

Steffen

Steve Rabbit
  • 45
  • 1
  • 5

2 Answers2

4

this is useful case, and I always try it source from 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;
    }
Ahmad Hindash
  • 1,519
  • 15
  • 16
0

First, you need to take value from that field. Each sharepoint field type, has it's own class representation in c#. When you have string value, it's easy but when you have relation you need to use SPFieldLookup, etc.

When you have string, you can write something like that:

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

and then use

folderItem[internalFieldName] = stringFieldValue;

internalFieldName -> this is internal name of field, you can check it by go to the list and sort by this field, and you will have it name in querystring (url)

How to get lookup value from SpListItem

Community
  • 1
  • 1
zchpit
  • 3,071
  • 4
  • 28
  • 43
  • Can you please write the code more in "IF THEN ELSE" Syntax? I dont understand the "?" and ":" syntax.... :) – Steve Rabbit Oct 02 '13 at 13:50
  • string stringFieldValue = string.Empty; if(sourceItem[internalFieldName] != null) { stringFieldValue = currentItem[internalFieldName].ToString()' } else { stringFieldValue = string.Empty; } – zchpit Oct 02 '13 at 14:05