您的位置:首页 > 其它

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

2011-09-19 10:42 344 查看

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

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

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

staticvoidAddNewItem()
{
using(SPSitesite=newSPSite("http://server"))
{
using(SPWebweb=site.OpenWeb())
{
SPListlist=web.GetList("Lists\\Jobs");
SPListItemitem=list.AddItem();
item["Title"]="SharePointDeveloper";
item["JobDescription"]="Goodat<h1>communication</h1>";
item["City"]="Shenzhen";
item["DueDate"]=DateTime.Now.AddMonths(1);
//Lookupfield
SPFieldLookupValueJobRequirement=newSPFieldLookupValue(1,"SharePoint");
item["JobRequirement"]=JobRequirement;
//PeopleandGroupField
SPFieldUserValueManager=newSPFieldUserValue(web,web.EnsureUser("domain\\alias").ID,"UserName");
item["Manager"]=Manager;
item.Update();
}
}
}


.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}

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

publicstringAddNewItem()
{
WSLists.ListslistsWS=newLists();
listsWS.Credentials=CredentialCache.DefaultCredentials;
listsWS.Url="http://servername/_vti_bin/lists.asmx";
XmlDocumentdoc=newXmlDocument();
XmlElementbatch=doc.CreateElement("Batch");
batch.SetAttribute("OnError","Continue");
batch.SetAttribute("ListVersion","1");
batch.SetAttribute("ViewName","{707B3736-82E4-4272-9E00-3A5163AD6ACD}");
stringtitle="SharePointProjectManager";
stringJobDescription="PMForSharePoint";
stringCity="Beijing";
stringDueDate=DateTime.Now.AddDays(15).ToString("yyyy-MM-ddThh:mm:ssZ");
stringJobRequirement="1;#SharePoint";
stringManager="1;#Name";
batch.InnerXml=string.Format("<MethodID='1'Cmd='New'>"
+"<FieldName='ID'>New</Field>"
+"<FieldName='Title'>{0}</Field>"
+"<FieldName='JobDescription'>{1}</Field>"
+"<FieldName='City'>{2}</Field>"
+"<FieldName='DueDate'>{3}</Field>"
+"<FieldName='JobRequirement'>{4}</Field>"
+"<FieldName='Manager'>{5}</Field>"
+"</Method>",
title,JobDescription,City,DueDate,JobRequirement,Manager);
XmlNodenodeResult=listsWS.UpdateListItems("da4000b2-d13e-4420-b7bf-176ab85d91ee",batch);
returnnodeResult.OuterXml;
}


.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}

3.客户端对象模型。SharePoint提供了三种对象模型:client.NetOM,SilverlightOM以及ECMAScriptOM.这里仅仅是一个Client.NETOM实例的一个演示。

publicvoidAddNewItem()
{
ClientContextctx=newClientContext(http://servername);
varjobsList=ctx.Web.Lists.GetByTitle("jobs");
ListItemCreationInformationitemCreateInfo=newListItemCreationInformation();
ListItemnewItem=jobsList.AddItem(itemCreateInfo);
newItem["Title"]="Webarchitecture";
newItem["JobDescription"]="<fontcolor='red'>architecture</font>";
newItem["City"]="Shanghai";
newItem["DueDate"]=DateTime.Now.AddDays(5);
newItem["JobRequirement"]="3;#";
newItem["Manager"]="1;#";
newItem.Update();
ctx.ExecuteQuery();
}
文章来源:/article/7036209.html


.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐