开发者

Get content type columns/metadata for an item

How to get content type columns or metadata for an item in SharePoint document library?

This links gives the file properties that i don't need http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfile.properties.aspx

I only to want to get content type co开发者_高级运维lumns for an item. I tried this string strXML = oItem.Xml.ToString(); but it gives me same garbage.


You can use the ContentType property of an SPListItem. If you want all content types in a list, you can use the ContentTypes property of an SPList. Once you have a content type reference, you can check its Fields property to get the columns.

Content Types for a list item:

SPContentType contentType = myListItem.ContentType;

foreach (SPField field in contentType)
{
    // Do your stuff with this column
}

Content Types for a list:

SPContentTypeCollection contentTypes = myList.ContentTypes;
List<object> values = new List<object>();
List<SPContentTypeId> blackList = new List<SPContentTypeId>()
{
    SPBuiltInContentTypeId.System,
    SPBuiltInContentTypeId.Item,
};

var goodContentTypes = contentTypes.Where(c => !blackList.Contains(c.Id));

foreach (SPContentType contentType in goodContentTypes)
{
    foreach (SPField field in contentType.Fields)
    {
        // Do your stuff with this column e.g. Get value from item
        values.Add(myListItem[field.InternalName]);
    }
}


Get the list that represents your document library. Once you have this you can look at the columns. I typically use this method:

SPWeb web = SPContext.Current.Web;  
SPList list = web.Lists["Your list name"];  
foreach (String field in list.DefaultView.ViewFields)  
{  
   //whatever
}

Which I believe gives you all the columns for the default view. You can also use this method:

SPWeb web = SPContext.Current.Web;  
SPList list = web.Lists["Your list name"];  
foreach (SPField field in list.Fields)  
{  
   //whatever
}

Which will give you info for all fields in the list (library) regardless of the view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜