[英]SharePoint : How can I programmatically add items to a custom list instance
我真的在尋找一個小的代碼片段,或者一個關於這個主題的好教程。
我有一個 C# 控制台應用程序,我將使用它以某種方式將列表項添加到我的自定義列表中。 我也創建了自定義內容類型。 所以不確定我是否也需要從這個內容類型創建一個 C# 類。 也許不是。
提前致謝
我認為這兩篇博文應該可以幫助您解決問題。
http://blog.the-dargans.co.uk/2007/04/programmatically-adding-items-to.html http://asadewa.wordpress.com/2007/11/19/adding-a-custom-content -type-specific-item-on-a-sharepoint-list/
短途游:
向列表中添加一個新項目:
SPListItem newItem = list.AddItem();
要將新項目綁定到內容類型,您必須為新項目設置內容類型 ID:
newItem["ContentTypeId"] = <Id of the content type>;
設置在您的內容類型中指定的字段。
提交您的更改:
newItem.Update();
簡單來說,您需要按照步驟操作。
假設列表名稱是Test並且它只有一個字段“標題”,這里是代碼。
using (SPSite oSite=new SPSite("http://mysharepoint")) { using (SPWeb oWeb=oSite.RootWeb) { SPList oList = oWeb.Lists["Test"]; SPListItem oSPListItem = oList.Items.Add(); oSPListItem["Title"] = "Hello SharePoint"; oSPListItem.Update(); } }
請注意,您需要在安裝了 SharePoint 的同一台服務器中運行此應用程序。
您不需要為自定義內容類型創建自定義類
您可以在自定義 SharePoint 列表中創建一個項目,執行如下操作:
using (SPSite site = new SPSite("http://sharepoint"))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists["My List"];
SPListItem listItem = list.AddItem();
listItem["Title"] = "The Title";
listItem["CustomColumn"] = "I am custom";
listItem.Update();
}
}
使用 list.AddItem() 應該保存被枚舉的列表項。
這就是 Microsoft 網站上的情況,我只是調整了 SPSite 和 SPWeb,因為它們可能因環境而異,因此不必對這些進行硬編碼:
using (SPSite oSiteCollection = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb oWeb = oSiteCollection.OpenWeb(SPContext.Current.Web))
{
SPList oList = oWeb.Lists["Announcements"];
// You may also use
// SPList oList = oWeb.GetList("/Lists/Announcements");
// to avoid querying all of the sites' lists
SPListItem oListItem = oList.Items.Add();
oListItem["Title"] = "My Item";
oListItem["Created"] = new DateTime(2004, 1, 23);
oListItem["Modified"] = new DateTime(2005, 10, 1);
oListItem["Author"] = 3;
oListItem["Editor"] = 3;
oListItem.Update();
}
}
來源:SPListItemClass (Microsoft.SharePoint)。 (2012)。 2012 年 2 月 22 日檢索自http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.aspx 。
我遇到了類似的問題,並且能夠通過以下方法解決它(類似於其他答案,但也需要憑據),
1-通過工具添加Microsoft.SharePointOnline.CSOM->NuGet Package Manager->Manage NuGet Packages for solution->Browse->選擇並安裝
2- 添加“使用 Microsoft.SharePoint.Client;”
然后下面的代碼
string siteUrl = "https://yourcompany.sharepoint.com/sites/Yoursite";
SecureString passWord = new SecureString();
var password = "Your password here";
var securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
ClientContext clientContext = new ClientContext(siteUrl);
clientContext.Credentials = new SharePointOnlineCredentials("Username@domain.nz", securePassword);/*passWord*/
List oList = clientContext.Web.Lists.GetByTitle("The name of your list here");
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem oListItem = oList.AddItem(itemCreateInfo);
oListItem["PK"] = "1";
oListItem["Precinct"] = "Mangere";
oListItem["Title"] = "Innovation";
oListItem["Project_x0020_Name"] = "test from C#";
oListItem["Project_x0020_ID"] = "ID_123_from C#";
oListItem["Project_x0020_start_x0020_date"] = "2020-05-01 01:01:01";
oListItem.Update();
clientContext.ExecuteQuery();
請記住,您的字段可能與您看到的不同,例如在我的列表中我看到“項目名稱”,而實際值為“Project_x0020_ID”。 如何獲得這些值(即內部歸檔值)?
幾種做法:
1- 使用 MS 流程並查看它們
2- https://mstechtalk.com/check-column-internal-name-sharepoint-list/或https://sharepoint.stackexchange.com/questions/787/finding-the-internal-name-and-display-name -for-a-list-column
3- 使用 C# 閱讀器並閱讀您的共享點列表
其余操作(更新/刪除): https : //docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ee539976(v%3Doffice.14)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.