Search code examples
c#sharepointmoss

Retrieve metadata column fields


I need to programmatically retrieve the columns in a Sharepoint document library, in order to set file properties externally to Sharepoint.

I've found that setting the metadata property is not hard as long as you already know the name of the column, which I cannot expect users to input themselves.

As it does not seem possible to do this through the Sharepoint Web Services I have created my own custom web service so I have access to the Client Object Model.

List Custom Columns

Using this code I am able to retrieve the custom columns I have created, however I am not able to distinguish between the ones editable in the item properties section (picture above) and those which aren't.

SPList list = web.Lists[specificList];
foreach (SPField field in list.Fields)
{
    if (!field.Hidden)
    {
        var title = field.Title;
        var description = field.Description;
        var parentList = field.ParentList;

        var references = field.FieldReferences; // contains names of fields referenced in computed fields

        if (references != null)
        {
            foreach (string reference in references)
            {
                var test = parentList.Fields.GetField(reference);
            }
        }
    }
}

I get extra properties such as:

  • Copy Source
  • Content Type
  • Checked Out To
  • Checked In Comment
  • Type
  • File
  • Size
  • Edit
  • Version
  • Source Version
  • Source Name

I have also tried retrieving the column fields from the SPFolder item, but again this returns many extra properties and is even less filterable.

foreach (SPListItem folderItem in list.Folders)
{
    SPFolder folder = folderItem.Folder;
    System.Collections.Hashtable oHashtable = folder.Properties;
    System.Collections.ICollection collKeys = oHashtable.Keys;

    foreach (var key in collKeys)
    {
        string keyName = key.ToString();
    }
}

Is there a standard way to retrieve the column fields I need? Or will I have to manually exclude the defaults ones such as "Checked out to"?


Solution

  • First you have to know which form you are viewing. Is it the EditForm or NewForm?

    You can filter the columns visible on a specific form by getting the fields of the ContentType and then check if they are getting displayed on the NewForm (or whatever form):

    SPList list = web.Lists[specificList];
    var contentType = list.ContentTypes[0]; // Select first contenttype. Change this if you need a different contentType
    foreach (SPField field in contentType.Fields)
    {
        if (!field.Hidden 
            && (field.ShowInEditForm == null
                || !field.ShowInEditForm.Value)) // Replace ShowInEditForm with the form you need
        {
            var title = field.Title;
            var description = field.Description;
            var parentList = field.ParentList;
    
            var references = field.FieldReferences; // contains names of fields referenced in computed fields
    
            if (references != null)
            {
                foreach (string reference in references)
                {
                    var test = parentList.Fields.GetField(reference);
                }
            }
        }
    }