A. Bernhagen
A. Bernhagen

Reputation: 11

Programmatically created document set item is always a folder

I have a problem by creating a list item programmatically. The szenario:

All items, i created manually in the Risk list are shown. Now I can add additional items from the new/edit form of the Changes list using the Add button. I can insert but the item is not shown in the grid.

Risk items within new form of changes list:

enter image description here

Why? If i look into my Risk list, I can see the inserted item, but it is not a item, it is shown as folder type.

Add new risk item:

enter image description here

This is my implementation to add the item:

SPListItem item = list.Items.Add();
item["ChangeReference"] = "1";
item[SPBuiltInFieldId.Title] = title.Text;
item["Title"] = title.Text;
item["Risk"] = risk.Text;
item["Probability"] = probability.Text;
item.Update();

The List Definition only has my custom content type:

<List xmlns:ows="Microsoft SharePoint" Title="Risks" FolderCreation="FALSE" Direction="$Resources:Direction;" EnableContentTypes="TRUE" Url="Lists\Risks" BaseType="0" xmlns="http://schemas.microsoft.com/sharepoint/">
  <MetaData>
    <ContentTypes>
      <ContentTypeRef ID="0x0108009007F1B9465C48F2AE80B77CC58A4AA7" />
    </ContentTypes>
...

The manually created item can be found within list.Items, the programmitcally created item are always created in list.folders, but I don't know why and how to change that? I need normal items, not folders.

Manually vs. prog items:

enter image description here

Any idears?

UPDATE:

I already try to set the CT, without success. I now use a standard task list without custom content type, but it always created a folder instead an item.

While debugging, everything looks fine:

Debugging item creation

Upvotes: 0

Views: 1406

Answers (3)

Sergey Borysenko
Sergey Borysenko

Reputation: 26

Try to add item to RootFolder.Items instead:

SPListItem item = list.RootFolder.Items.Add();

Upvotes: 0

A. Bernhagen
A. Bernhagen

Reputation: 11

I can't find a solution for that problem, so I use the workaround with CSOM.

var clientContext = new ClientContext(SPContext.Current.Web.Url);
var oList = clientContext.Web.Lists.GetByTitle("Risks");
var itemCreateInfo = new ListItemCreationInformation();
var oListItem = oList.AddItem(itemCreateInfo);
oListItem["Title"] = title.Text;
oListItem["Risk"] = risk.Text;
oListItem["Probability"] = probability.Text;
oListItem["Severity"] = severity.Text;
oListItem["ChangeReference"] = int.Parse(this.Page.Request.QueryString["ItemId"]);
oListItem.Update();
clientContext.Load(oListItem);
clientContext.ExecuteQuery();

Upvotes: 1

Gautam Sheth
Gautam Sheth

Reputation: 2500

Looks like your content type is not getting set whenever you add a list item.

Assuming that your content type name is Example Content Type, you can try the below code. Modify it as per your content type name:

SPContentType spctExampleContentType = list.ContentTypes["Example Content Type"];

SPListItem item = list.Items.Add();
item["ChangeReference"] = "1";
item[SPBuiltInFieldId.Title] = title.Text;
item["Title"] = title.Text;
item["Risk"] = risk.Text;
item["Probability"] = probability.Text;
spliNewItem["ContentTypeId"] = spctExampleContentType.Id;
item.Update();

Upvotes: 0

Related Questions