0

I'm trying to add new items into a Sharepoint List, using a winforms C# app, but I'm getting an error as if the app didn't find the list fields.

My code:

using SPC = Microsoft.SharePoint.Client;

(...)

string siteUrl = "https://sharepoint.company.com/sites/ProjectX";

SPC.ClientContext clientContext = new SPC.ClientContext(siteUrl);

string userName = "someone.surname";
SecureString password = new SecureString();
foreach (char c in "MyPaSsWoRd".ToCharArray()) password.AppendChar(c);
clientContext.Credentials = new NetworkCredential(userName, password, "MyDomain");

SPC.Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();

SPC.List oList = web.Lists.GetByTitle("List Y");
clientContext.Load(oList);
clientContext.ExecuteQuery();

SPC.ListItemCreationInformation listCreationInformation = new SPC.ListItemCreationInformation();

SPC.ListItem oListItem = oList.AddItem(listCreationInformation);

oListItem["field 1"] = "a";
oListItem["field 2"] = "b";
oListItem["user"] = "someone.surname";
oListItem["date 1"] = "01/01/2015";
oListItem["field 3"] = "99";

oListItem.Update();
clientContext.ExecuteQuery();

Error ocurring in the last code line:

Column 'field 1' does not exist. It may have been deleted by another user. /sites/ProjectX/Lists/List_Y

Any tip?

PS.: Yes, there is a field named "field 1" on the "List Y".

Thanks!

MetalicUNQ
  • 26
  • 2
  • 5
  • Maybe you should [write out the field names](http://stackoverflow.com/questions/12892430/sharepoint-client-object-model-how-to-get-all-the-fields-in-a-list) for your list, to ensure "field 1" is present in the oList object. – wooters Mar 11 '15 at 18:37
  • Thanks @wooters but I've done that already, using the line codes below and all field names are ok: `SPC.FieldCollection FLDlist = oList.Fields; clientContext.Load(FLDlist); clientContext.ExecuteQuery(); string mmmsg = string.Empty; foreach (SPC.Field fld in FLDlist) { mmmsg += fld.Title + Environment.NewLine; } MessageBox.Show(mmmsg);` – MetalicUNQ Mar 12 '15 at 19:49
  • Have a look at this [SO post](http://stackoverflow.com/questions/15790926/client-object-model-get-item-by-id-item-does-not-exist-it-may-have-been-delete), if you haven't already. – wooters Mar 12 '15 at 20:39
  • Sorry @wooters but I dont understand this: "sharepoint ask you about 'trust' then u have to choose the list wich u want to operate with". I cant see any tipe of "asking" or message from sharepoint. – MetalicUNQ Mar 17 '15 at 13:47
  • Yeah to be honest, I don't fully understand it either. It just looked directly related to the error that you are dealing with. – wooters Mar 17 '15 at 14:19
  • no problem @wooters, I thank you anyway! – MetalicUNQ Mar 18 '15 at 15:10

1 Answers1

0

I found the issue cause: I was using the "Display Name" of the fields, but I need to use the "Internal Name" instead.

So I checked in the List settings the Internal Name of the fields and then I used at this part of the code:

oListItem["field 1"] = "a";
oListItem["field 2"] = "b";
oListItem["user"] = "someone.surname";
oListItem["date 1"] = "01/01/2015";
oListItem["field 3"] = "99";

For example, the Internal Name of the field "field 1" is "f1"; the Internal Name of the "field 2" is "f2"; the Internal Name of the field "date 1" is "date_1"; and so on...

So, when I changed the Display Name into Internal Name (at the "oListItem["..."]), the error message "Column 'field 1' does not exist" doesn't ocurred anymore, because the fields are now found.

Other change I had to do was at this point:

oListItem["user"] = "someone.surname";

This way doesn't works (set the username directly). I had to use a variable to get the current user. This way:

oListItem["user"] = oUser.Id.ToString();

Where the "oUser" variable is:

SPC.User oUser = oWeb.CurrentUser;
clientContext.Load(oUser);
clientContext.ExecuteQuery();

That's it! It's all right now :) Thanks everybody!

MetalicUNQ
  • 26
  • 2
  • 5