0

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

  • 2 Lists (Changes-CustomDocSet Lib, Risks-Custom Task List) with custom content types and fields

  • The new/edit form of the Changes List includes a DataGrid witch shows the items of the Risks list

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

4

3 回答 3

1

我找不到该问题的解决方案,因此我使用 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();
于 2017-12-01T20:15:36.557 回答
0

每当您添加列表项时,您的内容类型似乎都没有设置。

假设您的内容类型名称是Example Content Type,您可以尝试以下代码。根据您的内容类型名称修改它:

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();
于 2017-11-15T09:24:13.687 回答
0

尝试将项目添加到 RootFolder.Items :

SPListItem item = list.RootFolder.Items.Add();
于 2017-12-02T08:54:25.600 回答