如何向列表中添加数据值(开发篇)

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

作为开发人员添加列表项的方式主要有如下几种:(服务器端)对象模型,客户端对象模型以及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:  }

你可能感兴趣的:(开发)