Blog Stats
  • Posts - 142
  • Articles - 0
  • Comments - 23
  • Trackbacks - 0

 

Manipulating SharePoint Lists with Code

How do I create a custom list?

   1: Guid listId = webSite.Lists.Add("Sample", "Sample List", SPListTemplateType.GenericList);
   2:               
How to determine if a list already exists within an SPWeb?
   1: public static bool DoesListExist(SPWeb web, String listName)
   2:        {
   3:            foreach (SPList list in web.Lists)
   4:            {
   5:                if (true == list.Title.Equals(listName, StringComparison.OrdinalIgnoreCase))
   6:                    return true;
   7:            }
   8:            return false;
   9:        }

How to determine if a column (field) already exists?

   1: if (!list.Fields.ContainsField("Description"))
   2:                     list.Fields.Add("Description", SPFieldType.Note, false);

How to add a lookup column to a list?

   1: Guid listGuid = webSite.Lists["lookupList"].ID;
   2:                 Guid listId = webSite.Lists.Add("Sample", "Sample List", SPListTemplateType.GenericList);
   3:                 SPList list = webSite.Lists[listId] as SPList;
   4:                 list.Fields.AddLookup("Sample Lookup", listGuid, true);
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Feedback

# re: Manipulating SharePoint Lists with Code

Gravatar I love how clear and simple the example code and explanation are for such an important task to achieve. 11/7/2008 4:17 PM | Tom Resing

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

 

 

Copyright © DennisBottjer