3

I would like to create a link list, and then add a choice column (Field) with two choices to the default view.

Using the following code i am able to create the list and add a column (Field) to the default view. I cant get the //CREATE FIELD part working:

// CREATE NEW LIST 
        ListCreationInformation newList12 = new ListCreationInformation();
        // Titel
        newList12.Title = "Links";
        newList12.TemplateType = (int)ListTemplateType.Links; // Template
        newList12.QuickLaunchOption = QuickLaunchOptions.Off; // Quicklaunch option
        List list12 = web.Lists.Add(newList12); 
        newList12.Description = "Opret og del links";
        // Update list + execute
        list12.Update();
        context.ExecuteQuery();

        //CREATE FIELD
        List listUpdate12 = web.Lists.GetByTitle("Links");
        listUpdate12.Fields.Add("Field Name", FieldType.Choice, false);

        //Get Field Reference
        FieldChoice col = (FieldChoice)listUpdate12.Fields.GetByTitle["Field Name"];

        col.Choices.Add("option 1");
        col.Choices.Add("option 2");
        col.DefaultValue = "option 1"; //otherwise blank row by default.

        col.update();  //Saves choices to column

        listUpdate12.Update();


        // ADD FIELD TO VIEW
        View view12 = list12.Views.GetByTitle("All Links");
        view12.ViewFields.Add("Field Name");

        // update view + list
        view12.Update();
        list12.Update();
        context.ExecuteQuery();

When i hower the "listUpdate12.Fields.Add("Field Name", FieldType.Choice, false);" part i get: Error: No overload method "Add" takes 3 arguments.

When i hower the "listUpdate12.Fields.GetByTitle["Field Name"];" part i get: Cannot apply indexing with [] to an expression of type 'method group'.

Any suggestions?

3
  1. You'll want to use the SPFieldType class instead of FieldType. Update: Actually, the issue is that the CSOM doesn't support that version of Add. Use AddAsXml instead:

    list.Fields.AddFieldAsXml("",true, AddFieldOptions.DefaultValue);

  2. When you call GetByTitle you're using [] - square brackets. Try using parenthesis to call the method instead - GetByTitle("Field Name")

Hope this helps!

  • I tried using the SPFieldType. I get the error: The name 'SPFieldType' does not exist in the current context. ?? The parenthesis helped, thanks! – Jacob Filtenborg Aug 20 '14 at 7:41
  • Ah, sorry, I didn't notice this was using the CSOM. What you'll want to do is use the AddFieldAsXml function instead of add: list.Fields.AddFieldAsXml("<Field DisplayName='MyField2' Type='Choice' />", true, AddFieldOptions.DefaultValue); – Ted N. Aug 20 '14 at 15:13
  • Thank you, thats much better, almost there! Only one problem left: The col.Choices.Add("option 1"); parts, doesnt work. When i hower .Add i get: 'System.Array' does not contain a definitions for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Array' could be found (Are you missing a using directive og an assembly?). Any ideas? – Jacob Filtenborg Aug 21 '14 at 8:16
  • Yep, so the reason that's happening is that FieldChoice.Choices is of the type string[] - arrays in C# have fixed length, so they don't support the Add method. What you probably want to do instead is just re-initialize Choices like so: col.Choices = new string[] { "option 1","option 2" }; – Ted N. Aug 21 '14 at 14:21
  • Okay nice, seems to work! However it breaks at: FieldChoice col = (FieldChoice)listUpdate12.Fields.GetByTitle("Field Name"); With message: An unhandled exception of type 'System.InvalidCastException' occurred in CreateList.exe Additional information: An object of type 'Microsoft.SharePoint.Client.Field' can not be converted to type 'Microsoft.SharePoint.Client.FieldChoice'. – Jacob Filtenborg Aug 22 '14 at 8:37

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.