最近由于工作的需要,接触了sharepoint的二次开发。关于一些二次开发的基础知识中文资料已经很多了,所以在此我就记录一些我在工作中遇到的问题和解决方法。
再做一个自定义字段的时候要在网站集下搜索所有的个人博客站点列出与文档库中某一文档相关的博客文章,并可以就当前浏览的文档在自己的网站中添加新的一篇博客。这个功能是由一个链接来实现的,所以就要用对象模型来获取到当前个人网站中博客网站的地址。
首先我们先在vs2005中新建一个classlibary的项目(BlogSearch),建立两个类文件SearchResult和SearchResultRender类。添加必要的引用。SearchResult的源码如下:
1using System;
2using System.Collections.Generic;
3using System.Text;
4using Microsoft.SharePoint;
5using Microsoft.SharePoint.WebControls;
6using Microsoft.SharePoint.Security;
7
8namespace BlogSearch
9{
10 public class SearchResults:SPFieldMultiLineText
11 {
12 public SearchResults(SPFieldCollection fields, string fieldname)
13 : base(fields, fieldname)
14 {
15
16 }
17 public SearchResults(SPFieldCollection fields, string typeName, string displayName)
18 : base(fields, typeName, displayName)
19 {
20
21
22 }
23
24 public override BaseFieldControl FieldRenderingControl
25 {
26 get
27 {
28 BaseFieldControl fieldcontrol = new SearchResultsControl();
29 fieldcontrol.FieldName = this.InternalName;
30 return fieldcontrol;
31 }
32 }
33 }
34}
这些代码没有特别之处都是建立自定义控件的基础,如有问题请参阅msdn和一步一步sharepoint2007的相关内容。
接下来就是我们这次要谈论的重点了,在sharepoint中每一个网站都会有一个自己的宿主网站集,这些你可以在sharepoint的管理中心中进行设置(管理中心->应用程序管理->office sharepoint server共享服务->创建或配置此服务器场的共享服务)。每一个网站集都会有自己的用户配置文件(该配置文件是在用户添加到sharepoint站点中的时候就已经建立了,可以在管理中心中查看所有的用户配置文件,当然你要有足够的权限:P),那么这些配置文件的信息可以用对象模型获得么?答案是肯定的。网站下所有的用户配置文件我们可以用Microsoft.Office.Server.UserProfiles.UserProfileManager获得(努力学习的小熊博客中有关于UserProfile操作的文章,不错的哦)。我们可以通过UserProfileManager.MySiteHostUrl获得当前网站集的宿主网站集的url。这就是我们想要得到的东东了(在这里汗自己一个,为了这个东东找了两个小时!!),废话不多说了直接上代码(SearchResultRender):
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Runtime.InteropServices;
5using System.Web.UI.WebControls;
6using System.Web;
7using System.Data.Sql;
8using System.Data.SqlClient;
9using System.Data;
10
11using Microsoft.SharePoint;
12using Microsoft.SharePoint.WebControls;
13
14using Microsoft.Office.Server;
15using Microsoft.Office.Server.UserProfiles;
16
17
18namespace BlogSearch
19{
20 class SearchResultsControl : BaseFieldControl
21 {
22 protected Literal results;
23
24 protected override string DefaultTemplateName
25 {
26 get
27 {
28 return "SearchResultsControl";
29 }
30 }
31
32
33 public override object Value
34 {
35 get
36 {
37 EnsureChildControls();
38 return results.Text;
39 }
40 set
41 {
42 EnsureChildControls();
43
44 results.Text = value.ToString();
45 }
46 }
47
48 public override void Focus()
49 {
50 EnsureChildControls();
51 results.Focus();
52 }
53
54 protected override void CreateChildControls()
55 {
56 if (this.ControlMode == SPControlMode.Display)
57 {
58 if (Field == null)
59 return;
60 string ID = this.Page.Request.QueryString["ID"] == null ? "" : this.Page.Request.QueryString["ID"];
61 base.CreateChildControls();
62 results = new Literal(); ;
63 Controls.Add(results);
64 try
65 {
66 SPWeb web = SPContext.Current.Web;
67 SPList list = web.Lists["课程信息"];
68 SPListItem item = null;
69 if (ID != "")
70 {
71 item = list.Items.GetItemById(int.Parse(ID));
72 }
73
74 if (item == null)
75 throw new Exception("参数格式错误");
76 string className = item["名称"].ToString();
77 ServerContext context = ServerContext.GetContext(SPContext.Current.Site);
78 this.Context.Items["Microsoft.Office.ServerContext"] = context;
79 UserProfileManager UserManager = new UserProfileManager(context);
80 string hostUrl = UserManager.MySiteHostUrl;
81 SPSite site = new SPSite(hostUrl);
82 string baseName = site.ContentDatabase.Name;
83 DataTable tb=getBlogs(baseName, className);
84 results.Text = "<table style='width:100%'><tr><td><table style='width:100%'>";
85 if (tb.Rows.Count > 0)
86 {
87 foreach (DataRow dr in tb.Rows)
88 {
89 string url = hostUrl + dr["tp_dirName"].ToString() + "/post.aspx?ID=" + dr["tp_ID"].ToString();
90 results.Text += "<tr><td class=" + CssClass + "><a href='" + url + "' target='_blank'>" + dr["nvarchar1"].ToString() + "</a></td></tr>";
91 }
92 }
93 else
94 {
95 results.Text += "<tr><td class=" + CssClass + " valign=\"bottom\">暂无相关博客</td></tr>";
96 }
97
98 if (UserManager.UserExists(HttpContext.Current.User.Identity.Name))
99 {
100 UserProfile UserProfile = UserManager.GetUserProfile(HttpContext.Current.User.Identity.Name);
101 try
102 {
103 SPWeb blog = UserProfile.PersonalSite.OpenWeb().Webs["Blog"];
104 string url = blog.Url + "/Lists/Posts/NewPost.aspx";
105 string rootFolder =blog.RootFolder.ServerRelativeUrl;
106 string source = HttpUtility.UrlEncode(blog.Url + "/Lists/Posts/AllPosts.aspx");
107 results.Text += "</table></td><td valign=\"bottom\"><a href='" + url + "?Source=" + rootFolder + "&FromWeb="+HttpUtility.UrlEncode(web.Url)+"&List="+HttpUtility.UrlEncode("课程信息")+"&ID="+item.ID+"' class='" + CssClass + "' target='_blank'>添加相关博客</a></td></tr></table>";
108 }
109 catch
110 {
111 results.Text += "</table></td><td valign=\"bottom\" class=" + CssClass + ">尚未创建自己的博客,请先创建自己的博客!</td></tr></table>";
112 }
113 }
114 else
115 {
116 results.Text += "</table></td><td valign=\"bottom\" class=" + CssClass + ">您尚未创建自己的网站,请先创建网站后再添加相关博客!</td></tr></table>";
117 }
118
119 }
120
121 catch(Exception ex)
122 {
123 results.Text = ex.ToString();
124 }
125 }
126
127
128
129 }
130
131 protected override void Render(System.Web.UI.HtmlTextWriter output)
132 {
133 if (this.ControlMode == SPControlMode.Display)
134 {
135 results.RenderControl(output);
136 }
137 }
138
139 private DataTable getBlogs(string BaseName,string className)
140 {
141 string strConn = "server=(local);database="+BaseName+";uid=sa;pwd=P@ssw0rd";
142 SqlConnection conn = new SqlConnection(strConn);
143 SqlDataAdapter sda = new SqlDataAdapter("select * from AllUserData where tp_ContentType=N'文章' and nvarchar3=N'" + className + "'",conn);
144
145 DataSet ds = new DataSet();
146 sda.Fill(ds);
147 return ds.Tables[0];
148 }
149
150 }
151}
152
ok这样一个关于网站集下所有个人网站博客的博文搜索的自定义字段就搞定了。希望对大家有所帮助,第一次写技术性的东东,不到之处还请各位前辈见谅,我会努力改进的。这次就到这里吧!休息休息一下