0

In my feature activation i create list. But now i want to add new column to list withouth deleting list and recreating new one. This is my existing code to create list

using (SPSite site = new SPSite(siteRoot.Url))
        {
            using (SPWeb web = site.RootWeb)
            {
                try
                {

                    SPListCollection lists = web.Lists;
                    SPList listexist = web.Lists.TryGetList(listName);
                    if (listexist == null)
                    {

                        lists.Add(listName, "List to upload emp info", SPListTemplateType.GenericList);

                        SPList list = web.Lists[listName];
                        list.AnonymousPermMask64 = SPBasePermissions.EditListItems;

                        SPField deptField = list.Fields.GetFieldByInternalName("Title");
                        deptField.Title = "Contact Name";
                        deptField.PushChangesToLists = true;   // *** push to list
                        deptField.Update(true);            // *** using Update(true) updates list & DB
                        list.Update();


                        // create Text type new column called "My Column" 
                        //list.Fields.Add("Name", SPFieldType.Text, true);
                        list.Fields.Add("Email", SPFieldType.Text, true);
                        list.Fields.Add("Manager", SPFieldType.Note, true);

                        //Winnerstatus to show all the winners
                        list.Fields.Add("Winnerstatus", SPFieldType.Boolean, false);


                        // make new column visible in default view
                        SPView view = list.DefaultView;
                        view.ViewFields.Add("Email");
                        view.ViewFields.Add("Name");
                        view.ViewFields.Add("Manager");

                        view.Update();
                    }
                }
                catch (Exception ex)
                {

                }
            }
        }

There is something called list.Fields.ContainsField("NewColumn") to check if column exist or not. But what is better way to fit in this code. Because first i have this condition below

if listexist == null

so does it mean i have to add one more condition to check if listexist != null and then do check for column exist or not? Is there better way?

0
 private static bool Field_ISAlreadyExists(List newList, string Field_InternalName, ClientContext clientContext)
    {
        FieldCollection collFields = newList.Fields;
        clientContext.Load(collFields);
        clientContext.ExecuteQuery();

        // Check existing fields
        bool _IsAlreadyExists = false;

        if (Field_InternalName != "")
        {
            var fieldExists = collFields.Any(f => f.InternalName.Trim().ToLower() == Field_InternalName.Trim().ToLower());

            if (fieldExists)
            {
                _IsAlreadyExists = true;
                //Console.WriteLine("This Column " + Field_InternalName + " is already exist");
            }
        }
        else
        {
            _IsAlreadyExists = true;
            Console.WriteLine("Please enter correct list column detail.");
        }

        return _IsAlreadyExists;
    }

OR

    private static bool ContainsField(List list, string fieldName)
    {
        var ctx = list.Context;
        var result = ctx.LoadQuery(list.Fields.Where(f => f.InternalName == fieldName));
        ctx.ExecuteQuery();
        return result.Any();
    }

These two functions may help you. Here I am using CSOM approach.

|improve this answer|||||
  • where do you check if list exist or not? – Happy Oct 1 '15 at 5:35
0

Just try it like this:

SPListCollection lists = web.Lists;
SPList listexist = web.Lists.TryGetList(listName);
if (listexist == null){
  //do your stuff
}
else{
  //check if list already has this field, if not, add it.
  if(!listexist.Fields.ContainsField("NewColumn")){
        //add the column to your list
  }
}
|improve this answer|||||
  • Where i get list variable? – Happy Sep 30 '15 at 11:44
  • sorry corrected it – Gwny Sep 30 '15 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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