views:

39

answers:

1

I'm creating a program that uses SharePoint Web Services to query and show a Sharepoint list to the user. I can only show one column at a time, so I need to find a 'default column' or 'display column' to show. I know 'Title' is commonly used in many of the content types but I want this to be robust with any type of custom content type or list so I would like to find some way of querying the list to discover this field.

For example: I'm using SharePoint Manager 2010 here and looking at a Link Library (That doesn't have a Title field) but somehow it knows that the list item is called 'http://google.com'. How is it inferring this? alt text

+1  A: 

Looks like DisplayName has quite a bit of logic behind it. Here is the code I got using Reflector:

public string DisplayName
{
    get
    {
        if (!this.IsNew)
        {
            if ((!this.ParentList.AllowContentTypes && (this.ParentList.BaseType == SPBaseType.DocumentLibrary)) || (this.ParentList.AllowContentTypes && (this.ContentTypeId.IsNonDiscussionFolder || this.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.Document))))
            {
                string str = (string) this.GetValue("BaseName", false);
                if (!string.IsNullOrEmpty(str))
                {
                    return SPHttpUtility.HtmlDecode(str);
                }
            }
            SPField fieldByInternalName = this.Fields.GetFieldByInternalName("Title", false);
            if (fieldByInternalName != null)
            {
                string fieldValueAsText = fieldByInternalName.GetFieldValueAsText(this.GetValue(fieldByInternalName, -1, false));
                if (!string.IsNullOrEmpty(fieldValueAsText))
                {
                    return fieldValueAsText;
                }
            }
            if (this.ParentList.AllowContentTypes)
            {
                if (this.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.Link))
                {
                    SPFieldUrlValue value2 = new SPFieldUrlValue((string) this.GetValue("URL", false));
                    if (!string.IsNullOrEmpty(value2.Description))
                    {
                        return value2.Description;
                    }
                    if (!string.IsNullOrEmpty(value2.Url))
                    {
                        return value2.Url;
                    }
                }
                if (this.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.Message))
                {
                    Guid discussionTitleLookup = SPBuiltInFieldId.DiscussionTitleLookup;
                    SPField fld = this.Fields[discussionTitleLookup];
                    string str3 = fld.GetFieldValueAsText(this.GetValue(fld, -1, false));
                    if (!string.IsNullOrEmpty(str3))
                    {
                        return str3;
                    }
                }
            }
            if (this.ParentList.BaseType != SPBaseType.Survey)
            {
                using (IEnumerator enumerator = this.Fields.GetEnumerator())
                {
                    SPField field3;
                    string str5;
                    while (enumerator.MoveNext())
                    {
                        field3 = (SPField) enumerator.Current;
                        if (field3.GetFieldBoolValue("TitleField"))
                        {
                            goto Label_00C6;
                        }
                    }
                    goto Label_016F;
                Label_00BB:
                    if (!(field3 is SPFieldMultiLineText))
                    {
                        return str5;
                    }
                    goto Label_00ED;
                Label_00C6:
                    str5 = field3.GetFieldValueAsText(this.GetValue(field3, -1, false));
                    if (string.IsNullOrEmpty(str5))
                    {
                        goto Label_016F;
                    }
                    goto Label_00BB;
                Label_00ED:
                    if (str5.Length <= 0xff)
                    {
                        return str5;
                    }
                    return str5.Substring(0, 0xff);
                }
            }
            SPContext context2 = SPContext.Current;
            if ((context2 == null) || (context2.FormContext.FormMode != SPControlMode.Edit))
            {
                return SPResource.GetString("ViewResponseTitle", new object[] { this.ID.ToString("N0", this.Web.Locale) });
            }
            return SPResource.GetString("ToolBarMenuRespondToSurvey", new object[0]);
        }
        SPContext current = SPContext.Current;
        if (this.ParentList.BaseType != SPBaseType.Survey)
        {
            if ((current != null) && current.FormContext.IsNonDiscussionFolder)
            {
                return SPResource.GetString("ButtonTextNewFolder", new object[0]);
            }
            return SPResource.GetString("NewFormTitleNewItem", new object[0]);
        }
        return SPResource.GetString("ToolBarMenuRespondToSurvey", new object[0]);
    Label_016F:
        return SPResource.GetString("NoTitle", new object[0]);
    }
}
Kit Menke