Sharepoint2010创建webpart详细步骤
1.新建Sharepoint2010空白项目
2.部署为场解决方案
3.添加“新建项”
4.选择“web部件”
5.代码如下
usingSystem;
usingSystem.ComponentModel;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingMicrosoft.SharePoint;
usingMicrosoft.SharePoint.WebControls;
usingSystem.Text;
usingMicrosoft.SharePoint.WebPartPages;
usingSystem.Data;
namespaceStrong.PortalForAJF.CompanyLeader
{
[ToolboxItemAttribute(false)]
publicclassCompanyLeader: System.Web.UI.WebControls.WebParts.WebPart
{
Labellab = newLabel();
protectedoverridevoidCreateChildControls()
{
lab.Text = this.ShowInfo();
//调用外部css样式
CssRegistrationcssControls = newCssRegistration();
cssControls.Name = "/_layouts/Strong.PortalForAJF/MyStyle.css";
Page.Header.Controls.Add(cssControls);
this.Controls.Add(lab);
}
privatestring_caml = "<OrderBy><FieldRef Name=\"Title\" /> <FieldRef Name='_x5f00__x59cb__x65f6__x95f4_' /></OrderBy>";
privatestring_listname = "";
privatestringShowInfo()
{
if(this._listname != "")
{
try
{
intnum;
SPListlist = SPContext.Current.Web.Lists[this._listname];
stringdefaultViewUrl = list.DefaultViewUrl;
SPListItemCollectionitems = null;
if(this._caml == "")
{
items = list.Items;
}
else
{
SPQueryquery = newSPQuery();
query.Query = this._caml;
items = list.GetItems(query);
}
DataTablemyTable = newDataTable();
myTable.Columns.Add(newDataColumn("name"));
myTable.Columns.Add(newDataColumn("work"));
myTable.Columns.Add(newDataColumn("stime"));
myTable.Columns.Add(newDataColumn("etime"));
myTable.Columns.Add(newDataColumn("jingli"));
foreach(SPListItemtheItem initems)
{
DataRowtheRow = myTable.NewRow();
theRow["name"] = theItem["姓名"].ToString();
theRow["work"] = theItem["职务"].ToString();
theRow["stime"] = theItem["开始时间"].ToString();
theRow["etime"] = theItem["结束时间"].ToString();
theRow["jingli"] = theItem["主要工作经历"].ToString();
myTable.Rows.Add(theRow);
}
//去除重复字段的行,并保留一个
DataViewmyDataView = newDataView(myTable);
string[] strComuns = { "name", "work"};
DataTabletable = myDataView.ToTable(true, strComuns);
//用法:主要做的工作就是把存在重复行数据的DataTable送给Dataview.然后使用DataView类的一个ToTable方法中的一个重载方法。就是上面用的,一共有两个参数,第一个bool类型参数就是指定装换成DataTable后是否保留重复行,第二个参数是一个字符串数组,用来指定转换成DataTable后保留原有表中的哪些字段。字段名不区分大小写。
stringfromString = "<table style=\"border:1px\" width=\"100%\" bgcolor=\"#a5b5c0\" cellspacing=\"1\" cellpadding=\"0\">";
fromString += "<tr> <td class=\"dtit\">姓名</td> <td class=\"dtit\">职务</td> <td class=\"dtit\">主要工作经历</td></tr>";
for(inti = 0; i < table.Rows.Count; i++)
{
if(i%2 == 0)
{
fromString += "<tr><td align=\"center\" height=\"50\" valign=\"middle\" bgcolor=\"#EEFFF9\" style=\"width: 6%\" class=\"bigh\">";
fromString += table.Rows[i]["name"] + @"</td>";
fromString += "<td align=\"center\" height=\"50\" valign=\"middle\" bgcolor=\"#EEFFF9\" style=\"width: 7%\">";
fromString += table.Rows[i]["work"] + @"</td>";
fromString += "<td height=\"50\" valign=\"middle\" bgcolor=\"#EEFFF9\" style=\"width: 70%\"><ul class=\"zhiwu_ul\">";
for(intj = 0; j < myTable.Rows.Count; j++)
{
if(table.Rows[i]["name"].Equals(myTable.Rows[j]["name"]))
{
fromString += "<li>"+ Convert.ToDateTime(myTable.Rows[j]["stime"]).ToString("yyyy-MM") + @"至"+ Convert.ToDateTime(myTable.Rows[j]["etime"]).ToString("yyyy-MM") + " "+ myTable.Rows[j]["jingli"] + @"</li>";
}
}
fromString += @"</ul></td></tr>";
}
else
{
fromString += "<tr><td align=\"center\" height=\"50\" valign=\"middle\" bgcolor=\"#BDE5FF\" style=\"width: 6%\" class=\"bigh\">";
fromString += table.Rows[i]["name"] + @"</td>";
fromString += "<td align=\"center\" height=\"50\" valign=\"middle\" bgcolor=\"#BDE5FF\" style=\"width: 7%\">";
fromString += table.Rows[i]["work"] + @"</td>";
fromString += "<td height=\"50\" valign=\"middle\" bgcolor=\"#BDE5FF\" style=\"width: 70%\"><ul class=\"zhiwu_ul\">";
for(intj = 0; j < myTable.Rows.Count; j++)
{
if(table.Rows[i]["name"].Equals(myTable.Rows[j]["name"]))
{
fromString += "<li>"+ Convert.ToDateTime(myTable.Rows[j]["stime"]).ToString("yyyy-MM") + @"至"+ Convert.ToDateTime(myTable.Rows[j]["etime"]).ToString("yyyy-MM") + " "+ myTable.Rows[j]["jingli"] + @"</li>";
}
}
fromString += @"</ul></td></tr>";
}
}
fromString += @"</table>";
returnfromString;
}
catch(Exceptionexception)
{
returnexception.Message;
}
}
return"请正确选择列表和视图";
}
[Description("列表名称"), Personalizable, WebBrowsable, Category("列表设置"), DefaultValue(""), WebPartStorage(Storage.Shared), FriendlyName("列表名称")]
publicstringListName
{
get
{
returnthis._listname;
}
set
{
this._listname = value;
}
}
[Description("列表显示CAML"), Category("列表设置"), WebPartStorage(Storage.Shared), DefaultValue("<OrderBy><FieldRef Name=\"ID\" Ascending=\"FALSE\" /></OrderBy>"), FriendlyName("列表显示CAML"), Personalizable, WebBrowsable]
publicstringCAML
{
get
{
returnthis._caml;
}
set
{
this._caml = value;
}
}
}
}
6.图片保存在映射的images文件夹下
7.css样式文件保存到映射的layouts文件夹下
8.Css样式文件
body
{
}
<styletype="text/css">
<!--
.downloadstylea{font-weight:normal;text-decoration: none;color: #003399;
}
.shadow_left{
background-image: url('../_catalogs/masterpage/_layouts/images/cooec/shadow_left_v3.jpg');
background-repeat: repeat-y;
}
.shadow_right{
background-image: url('../_catalogs/masterpage/_layouts/images/cooec/shadow_right_v3.jpg');
background-repeat: repeat-y;
}
td{
font-size:12px;
}
.style1{
background-color: #F8F8F8;
}
/*
a:link{font-weight :normal;text-decoration : none ;color : #003399 ;}
a:visited {font-weight : normal;text-decoration : none ;color : #003399 ;}
a:hover {font-weight : normal;text-decoration : none ;color : #003399 ;}
a:active {font-weight : normal;text-decoration : none ;color : #003399 ;}
*/
body{
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
background-color:#93C0E9;
}
.ms-globalright{
float:right;
color:#ffffff;
unicode-bidi:embed;
font-size:9pt;
font-family:宋体;
line-height:10pt;
text-decoration:none
}
.top_Line_bg{
background-position: 50%bottom;
background-image: url('../_catalogs/masterpage/_layouts/IMAGES/ITDep/head_r1_c1.jpg');
background-repeat: repeat-x;
}
.head_bg
{
background-image: url('../_catalogs/masterpage/_layouts/IMAGES/ITDep/head_bg.jpg');
background-repeat: repeat-x;
background-position: centercenter;
font-size:12px;
text-align:center;
}
.main_td5
{
width: 9px;
background-image: url('../_catalogs/masterpage/Images/table_right_bg.jpg');
background-repeat: repeat-y;
}
.ms-sitemapdirectional,.ms-sitemapdirectionala{
unicode-bidi:embed;
font-size:9pt;
font-family:宋体;
line-height:10pt;
text-decoration:none
}
.ms-sitemapdirectional,.ms-sitemapdirectionala:active{
unicode-bidi:embed;
font-size:9pt;
font-family:宋体;
line-height:10pt;
text-decoration:none
}
.shadowRight
{
height:100%;
min-height:100%;
width:12px;
background-image: url('/_layouts/Images/COOEC/Shadow_Right.jpg');
background-repeat:repeat-y;
background-position:lefttop;
}
.shadowLeft
{
height:100%;
min-height:100%;
width:12px;
background-image:url('/_layouts/Images/COOEC/Shadow_Left.jpg');
background-repeat:repeat-y;
background-position:lefttop;
}
.style6{
background-image: url('../_catalogs/masterpage/_layouts/images/cooec/v1_head_r3_c1.jpg');
}
.style1{
text-align: right;
background-image: url('../_catalogs/masterpage/_layouts/IMAGES/COOEC/V1_HEAD_r1_c1_f2.jpg');
}
-->
.dborder{
border-top-width: 1px;
border-left-width: 1px;
border-top-style: solid;
border-left-style: solid;
border-top-color: #0489AA;
border-left-color: #0489AA;
font-family: "宋体";
font-size: 12px;
}
.dtit{
line-height: 35px;
background-image: url(/_layouts/images/Strong.PortalForAJF/tith.jpg);
background-repeat: repeat-x;
background-position: lefttop;
text-align: center;
height: 35px;
font-size: 14px;
font-weight: bold;
color: #FFFFFF;
font-family: "宋体";
letter-spacing: 0.5em;
border-right-width: 1px;
border-bottom-width: 1px;
border-right-style: solid;
border-bottom-style: solid;
border-right-color: #0489AA;
border-bottom-color: #0489AA;
}
.bigh{
font-family: "宋体";
font-size: 14px;
font-weight: bold;
color: #000000;
letter-spacing: 0.2em;
}
ul
{
font-size:12px;
}
.zhiwu_ul1{
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 2%;
border-top-width: 0px;
border-right-width: 0px;
border-bottom-width: 0px;
border-left-width: 0px;
text-align: left;
list-style-type: none;
margin-top: 10px;
margin-right: 0px;
margin-bottom: 10px;
margin-left: 0px;
font-family: "宋体";
font-size: 12px;
color: #333333;
}
.zhiwu_ulli{
background-image: url(/_layouts/images/Strong.PortalForAJF/jiantou.gif);
background-repeat: no-repeat;
background-position: left5px;
list-style-type: none;
text-align: left;
line-height: 20px;
padding-left: 34px;
margin: 0px;
border-top-width: 0px;
border-right-width: 0px;
border-bottom-width: 0px;
border-left-width: 0px;
}
.Leadertable{border-collapse:collapse;border:solid#999;border-width:1px001px;}
.Leadertabletd{
border-right-width: 1px;
border-bottom-width: 1px;
border-right-style: solid;
border-bottom-style: solid;
border-right-color: #0489AA;
border-bottom-color: #0489AA;
text-align: center;
}
tr.t1td{background-color:#EEFFF9;}/* 第一行的背景色*/
tr.t2td{background-color:#BDE5FF;}/* 第二行的背景色*/
tr.t3td{background-color:#FFFFCC;}/* 鼠标经过时的背景色*/
.style7{
background-repeat: repeat;
background-position: 00;
}
</style>
9.部署