Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

i need a method where add various Fields in a Sharepoint List, but these Fields must be in the parameter of this method.

Show me this method and how should I use it.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Something like this to get you going perhaps?

private static void AppendNewFieldToListIfNonExistsent(SPWeb web, SPList list, string internalName,
                                                           string displayName, SPFieldType fieldType,
                                                           string lookupList = null, string lookupField = null)
        if (list.Fields.ContainsField(internalName)) return;

        if (fieldType == SPFieldType.Lookup)
            var lookupListId = EnsureLookupEntities(web, lookupList, lookupField);
            list.Fields.AddLookup(internalName, lookupListId, false);
            list.Fields.Add(internalName, fieldType, false);
        var field = list.Fields.GetFieldByInternalName(internalName);
        field.Title = displayName;
        var view = list.DefaultView;
        if (view.ViewFields.Exists(field.InternalName)) return;

MSDN SPFIeldCollection.Add(SPField)
MSDN SPFieldCollection.Add(String, SPFieldType, Boolean)
MSDN SPFieldCollection.AddLookup Method (String,Guid,Boolean)
MSDN SPFieldCollection.AddLookup Method (String,Guid,Guid,Boolean)

share|improve this answer
Tks Man!!! But if this method uses a dictionary as a parameter passing the SPFields. –  fbadaro Oct 3 '12 at 20:27
Your method? In that case just loop through them. You could add SPField directly, but it's a bit odd I think because of the SPField constructors –  eirikb Oct 3 '12 at 21:08
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.