如何向列表中添加資料值(開發篇)

來源:互聯網
上載者:User

上一篇中,我列舉了幾種管理員或者一般使用者添加清單項目的方式。這一篇我將從開發人員的角度來完成這個操作。

作為開發人員添加清單項目的方式主要有如下幾種:(伺服器端)物件模型,用戶端物件模型以及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:  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.