locked
get the type of a list field RRS feed

  • Question

  • Hey,

    is it possible to get the type of an SharePoint list field without knowing the name of the field?

    I like to copy a list item between sitecollection. So I have to check if the field type managed metadata or people picker or other.

    Thanks for your help

    Stefan


    Viele Grüße Stefan

    Kontakt unter Info@IT-Kiessig.de

    Tuesday, February 19, 2013 8:29 PM

Answers

  • You should be able to check the fields begin checked is a SPField user. This allows you to user the SPFieldUserValue class to extract the value.

    Something like

    foreach (SPField f in sourceItem.Fields) {
      if (f.FieldValueType == typeof(SPFieldUserValue))
      {
      }
    }


    SharePoint Developer | @zeemanj | Blog

    Thursday, February 21, 2013 10:13 PM

All replies

  • You can use PowerShell to print out all the fields in a list, including the type of field.

    E.g.

    $web = Get-Spweb http://my
    $l = $web.Lists["test"]
    $l.Fields | sort title | FT Title,Type
    $web.Dispose()


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    Tuesday, February 19, 2013 8:40 PM
  • It is possible to copy list items using the following code sample. It requires the source and destination to use the same content type.

    private SPListItem CopyItem(SPListItem sourceItem, string destinationListName) { //Copy sourceItem to destinationList SPList destinationList = sourceItem.Web.Lists[destinationListName]; SPListItem targetItem = destinationList.Items.Add(); foreach (SPField f in sourceItem.Fields) { //Copy all except attachments. if (!f.ReadOnlyField && f.InternalName != "Attachments" && null != sourceItem[f.InternalName]) { targetItem[f.InternalName] = sourceItem[f.InternalName]; } } return targetItem; }



    SharePoint Developer | @zeemanj | Blog

    Tuesday, February 19, 2013 9:16 PM
  • Thanks for your help. I used this code from Jeffrey but it dont work with people picker field ( different site Collection).

    Viele Grüße Stefan

    Kontakt unter Info@IT-Kiessig.de

    Tuesday, February 19, 2013 10:34 PM
  • Is the issue an exception or is the field value not copied?


    SharePoint Developer | @zeemanj | Blog

    Wednesday, February 20, 2013 6:43 PM
  • Hey,

    the field value will copy. But there is no write value in the target field.

    Like copy one user in the people picker field between sitecollections (same by metadata fields). This is because there not use the same ids in different site collection.

    I have to check with “SPFieldType” for the right field type.

    Regarding’s

    Stefan


    Viele Grüße Stefan

    Kontakt unter Info@IT-Kiessig.de

    Thursday, February 21, 2013 5:29 PM
  • You should be able to check the fields begin checked is a SPField user. This allows you to user the SPFieldUserValue class to extract the value.

    Something like

    foreach (SPField f in sourceItem.Fields) {
      if (f.FieldValueType == typeof(SPFieldUserValue))
      {
      }
    }


    SharePoint Developer | @zeemanj | Blog

    Thursday, February 21, 2013 10:13 PM