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;
}
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.部署
轉載于:https://www.cnblogs.com/914556495wxkj/p/3523726.html