Sharepoint2010创建webpart详细步骤

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") + " &nbsp; &nbsp; &nbsp;"+ 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") + " &nbsp; &nbsp; &nbsp;"+ myTable.Rows[j]["jingli"] + @"</li>";

 

                                }

                            }

                            fromString += @"</ul></td></tr>";

                        }

 

 

                    }

 

 

 

 

                    fromString += @"</table>";

 

                    returnfromString;

                    

           

 

    

 

      

  

                }

                catch(Exceptionexception)

                {

                    returnexception.Message;

                }

            }

            return"请正确选择列表和视图";

        }

 

 

        [Description("列表名称"), PersonalizableWebBrowsableCategory("列表设置"), 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"), PersonalizableWebBrowsable]

        publicstringCAML

        {

            get

            {

                returnthis._caml;

            }

            set

            {

                this._caml = value;

            }

        }

    }

}

 

 

 

6.图片保存在映射的images文件夹下

 

7.css样式文件保存到映射的layouts文件夹下

 

8.Css样式文件

body

{

}

 

    <styletype="text/css"> 

<!--

.downloadstylea{font-weight:normal;text-decorationnone;color#003399;

    

    }

.shadow_left{

    background-imageurl('../_catalogs/masterpage/_layouts/images/cooec/shadow_left_v3.jpg');

    background-repeatrepeat-y;

}

.shadow_right{

background-image: url('../_catalogs/masterpage/_layouts/images/cooec/shadow_right_v3.jpg');

    background-repeatrepeat-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-left0px;

    margin-top0px;

    margin-right0px;

    margin-bottom0px;

    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-position50%bottom;

    background-imageurl('../_catalogs/masterpage/_layouts/IMAGES/ITDep/head_r1_c1.jpg');

    background-repeatrepeat-x;

}

.head_bg

{

    background-imageurl('../_catalogs/masterpage/_layouts/IMAGES/ITDep/head_bg.jpg');

    background-repeatrepeat-x;

    background-positioncentercenter;

    font-size:12px;

    text-align:center;

}

.main_td5

{

    width9px;

    background-imageurl('../_catalogs/masterpage/Images/table_right_bg.jpg');

    background-repeatrepeat-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-imageurl('/_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-imageurl('../_catalogs/masterpage/_layouts/images/cooec/v1_head_r3_c1.jpg');

}

.style1{

    text-alignright;

    background-imageurl('../_catalogs/masterpage/_layouts/IMAGES/COOEC/V1_HEAD_r1_c1_f2.jpg');

}

-->

 

.dborder{

    border-top-width1px;

    border-left-width1px;

    border-top-stylesolid;

    border-left-stylesolid;

    border-top-color#0489AA;

    border-left-color#0489AA;

    font-family"宋体";

    font-size12px;

}

.dtit{

    line-height35px;

    background-imageurl(/_layouts/images/Strong.PortalForAJF/tith.jpg);

    background-repeatrepeat-x;

    background-positionlefttop;

    text-aligncenter;

    height35px;

    font-size14px;

    font-weightbold;

    color#FFFFFF;

    font-family"宋体";

    letter-spacing0.5em;

    border-right-width1px;

    border-bottom-width1px;

    border-right-stylesolid;

    border-bottom-stylesolid;

    border-right-color#0489AA;

    border-bottom-color#0489AA;

}

.bigh{

    font-family"宋体";

    font-size14px;

    font-weightbold;

    color#000000;

    letter-spacing0.2em;

}

ul

{

    font-size:12px;

}

.zhiwu_ul1{

    padding-top0px;

    padding-right0px;

    padding-bottom0px;

    padding-left2%;

    border-top-width0px;

    border-right-width0px;

    border-bottom-width0px;

    border-left-width0px;

    text-alignleft;

    list-style-typenone;

    margin-top10px;

    margin-right0px;

    margin-bottom10px;

    margin-left0px;

    font-family"宋体";

    font-size12px;

    color#333333;

}

.zhiwu_ulli{

    background-imageurl(/_layouts/images/Strong.PortalForAJF/jiantou.gif);

    background-repeatno-repeat;

    background-positionleft5px;

    list-style-typenone;

    text-alignleft;

    line-height20px;

    padding-left34px;

    margin0px;

    border-top-width0px;

    border-right-width0px;

    border-bottom-width0px;

    border-left-width0px;

}

.Leadertable{border-collapse:collapse;border:solid#999;border-width:1px001px;}

.Leadertabletd{

    border-right-width1px;

    border-bottom-width1px;

    border-right-stylesolid;

    border-bottom-stylesolid;

    border-right-color#0489AA;

    border-bottom-color#0489AA;

    text-aligncenter;

}

tr.t1td{background-color:#EEFFF9;}/* 第一行的背景色*/

tr.t2td{background-color:#BDE5FF;}/* 第二行的背景色*/

tr.t3td{background-color:#FFFFCC;}/* 鼠标经过时的背景色*/

 

.style7{

                background-repeatrepeat;

                background-position00;

}

 

</style>

 

9.部署

 

posted @ 2014-01-17 11:20  914556495  阅读(358)  评论(0编辑  收藏  举报