惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy International News Feed
Simon Willison's Weblog
Simon Willison's Weblog
I
Intezer
Spread Privacy
Spread Privacy
The Hacker News
The Hacker News
P
Palo Alto Networks Blog
TaoSecurity Blog
TaoSecurity Blog
S
Secure Thoughts
Google Online Security Blog
Google Online Security Blog
H
Heimdal Security Blog
N
News | PayPal Newsroom
Attack and Defense Labs
Attack and Defense Labs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园 - 【当耐特】
Webroot Blog
Webroot Blog
小众软件
小众软件
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
PCI Perspectives
PCI Perspectives
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
Cloudbric
Cloudbric
AI
AI
WordPress大学
WordPress大学
博客园 - 聂微东
Jina AI
Jina AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 三生石上(FineUI控件)
Hacker News: Ask HN
Hacker News: Ask HN
H
Hacker News: Front Page
博客园 - Franky
V
V2EX
Schneier on Security
Schneier on Security
G
GRAHAM CLULEY
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
H
Help Net Security
量子位
S
Security @ Cisco Blogs
大猫的无限游戏
大猫的无限游戏
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Recorded Future
Recorded Future
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
C
Cisco Blogs
S
Security Affairs

博客园 - 范文轩

Sharepoint中的Feature Stapling功能 SharePoint 2010中的WebProvisioned Event Handler 如何向列表中添加数据值(开发篇补充REST) 如何向列表中添加数据值(管理员篇) 在SharePoint 2010中动态加载Visio Web Part 使用编程的方式来启动SharePoint的工作流 InfoPath 2010调用REST的一个小应用 SharePoint 2010 WSP包部署过程中究竟发生什么? 如何查看SharePoint 2010的CU版本 SharePoint 2010多语言包的安装 在SharePoint 2010中使用Linq时候,请注意特殊字符 自定义ASP.NET WebApplication中调用SharePoint2010的对象 在Infopath 2010中调用Web Service 谈谈SharePoint 2010的客户端对象模型的性能问题 给Document Set里面添加文件夹 给Chart Web Part 添加过滤功能 SharePoint 2010的Form认证的用户注册功能 SharePoint 调查列表的自定义错误页面 Reporting Services 2008 and SharePoint 2010
如何向列表中添加数据值(开发篇)
范文轩 · 2011-08-13 · via 博客园 - 范文轩

上一篇中,我列举了几种管理员或者一般用户添加列表项的方式。这一篇我将从开发者的角度来完成这个操作。

作为开发人员添加列表项的方式主要有如下几种:(服务器端)对象模型,客户端对象模型以及Web Service.

1. (服务器端)对象模型。这种是开发中最常见的,可以是一个控制台程序,也可以写到你的Web Part或者Event Handler里面

   1:  static void AddNewItem()
   2:          {
   3:              using (SPSite site = new SPSite("http://server"))
   4:              {
   5:                  using (SPWeb web = site.OpenWeb())
   6:                  {
   7:                      SPList list = web.GetList("Lists\\Jobs");
   8:                      SPListItem item = list.AddItem();
   9:   
  10:                      item["Title"] = "SharePoint Developer";
  11:                      item["JobDescription"] = "Good at <h1>communication</h1>";
  12:   
  13:                      item["City"] = "Shenzhen";
  14:                      
  15:                      item["DueDate"]=DateTime.Now.AddMonths(1);
  16:   
  17:                      //Lookup field
  18:                      SPFieldLookupValue JobRequirement = new SPFieldLookupValue(1, "SharePoint");
  19:                      item["JobRequirement"] = JobRequirement;                   
  20:   
  21:   
  22:                      //People and Group Field
  23:                      SPFieldUserValue Manager = new SPFieldUserValue(web, web.EnsureUser("domain\\alias").ID, "User Name");
  24:                      item["Manager"] = Manager;                    
  25:   
  26:                      item.Update();    
  27:                  }
  28:              }
  29:          }

2. Web Service篇.这里需要注意的是时间类型的格式“yyyy-MM-ddThh:mm:ssZ”。

   1:  public string AddNewItem()
   2:  {
   3:     WSLists.Lists listsWS = new Lists();
   4:     listsWS.Credentials = CredentialCache.DefaultCredentials;
   5:     listsWS.Url = "http://servername/_vti_bin/lists.asmx";
   6:   
   7:      XmlDocument doc = new XmlDocument();
   8:      XmlElement batch = doc.CreateElement("Batch");
   9:      batch.SetAttribute("OnError", "Continue");
  10:      batch.SetAttribute("ListVersion", "1");
  11:      batch.SetAttribute("ViewName", "{707B3736-82E4-4272-9E00-3A5163AD6ACD}");
  12:      string title = "SharePoint Project Manager";
  13:      string JobDescription = "PM For SharePoint";
  14:      string City = "Beijing";
  15:      string DueDate = DateTime.Now.AddDays(15).ToString("yyyy-MM-ddThh:mm:ssZ");
  16:      string JobRequirement = "1;#SharePoint";
  17:      string Manager = "1;#Name";
  18:      batch.InnerXml = string.Format("<Method ID='1' Cmd='New'>"
  19:                  + "<Field Name='ID'>New</Field>"
  20:                  + "<Field Name='Title'>{0}</Field>"
  21:                  + "<Field Name='JobDescription'>{1}</Field>"
  22:                  + "<Field Name='City'>{2}</Field>"
  23:                  + "<Field Name='DueDate'>{3}</Field>"
  24:                  + "<Field Name='JobRequirement'>{4}</Field>"
  25:                  + "<Field Name='Manager'>{5}</Field>"
  26:                  +"</Method>",
  27:                  title,JobDescription,City,DueDate,JobRequirement,Manager);
  28:      XmlNode nodeResult=listsWS.UpdateListItems("da4000b2-d13e-4420-b7bf-176ab85d91ee", batch);
  29:      return nodeResult.OuterXml;
  30:   }

3. 客户端对象模型。SharePoint提供了三种对象模型:client .Net OM, Silverlight OM 以及ECMAScript OM. 这里仅仅是一个Client .NET OM实例的一个演示。

   1:  public void AddNewItem()
   2:  {
   3:      ClientContext ctx = new ClientContext(http://servername);
   4:      var jobsList=ctx.Web.Lists.GetByTitle("jobs");
   5:   
   6:      ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
   7:      ListItem newItem=jobsList.AddItem(itemCreateInfo);
   8:      newItem["Title"] = "Web architecture";
   9:      newItem["JobDescription"] = "<font color='red'>architecture</font>";
  10:      newItem["City"] = "Shanghai";
  11:      newItem["DueDate"] = DateTime.Now.AddDays(5);
  12:      newItem["JobRequirement"] = "3;#";
  13:      newItem["Manager"] = "1;#";
  14:      newItem.Update();
  15:      ctx.ExecuteQuery();
  16:  }