几个常用的SharePoint对象模型的有用函数
最近在开发东莞国税网上办税报表系统,用到SharePoint2007+Infopath Service作为表单服务器,后台数据存储采用Oracle,其中涉及了管理模板的需求,要对SharePoint对象模型进行操作,研究了2天sharepoint sdk,代码完成如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using System.Collections;
using System.Collections.Generic;
namespace SPAdmin
{
public class SharePointHelper
{
/// <summary>
/// 获取SPListItem对象所在的SPFolder
/// </summary>
/// <param name="item">SPListItem对象</param>
/// <returns>SPListItem对象所在的SPFolder</returns>
public static SPFolder GetItemsFolder(SPListItem item)
{
string dirPath = SPUtility.GetUrlDirectory(item.Url);
SPList list = item.ParentList;
if(list.ParentWeb.ServerRelativeUrl!="/")
dirPath=string.Format("{0}/{1}",list.ParentWeb.ServerRelativeUrl,dirPath);
SPListItem folderItem = list.ParentWeb.GetListItem(dirPath);
if (folderItem != null)
return folderItem.Folder;
else
return null;
}
/// <summary>
/// 取SPEventReceiverType枚举值
/// </summary>
/// <returns></returns>
public static List<ListItem> ListEventReceiverTypeForEnum()
{
List<ListItem> list = new List<ListItem>();
foreach (int i in Enum.GetValues(typeof(SPEventReceiverType)))
{
ListItem listitem = new ListItem(Enum.GetName(typeof(SPEventReceiverType), i), i.ToString());
list.Add(listitem);
}
return list;
}
/// <summary>
/// 转换SPEventReceiverType枚举
/// </summary>
/// <param name="Entype"></param>
/// <returns></returns>
public static Microsoft.SharePoint.SPEventReceiverType GetEventReceiverTypeForList(string Entype)
{
return (SPEventReceiverType)Enum.Parse(typeof(SPEventReceiverType), Entype, true);
}
/// <summary>
/// 取SPListTemplateType枚举值
/// </summary>
/// <returns></returns>
public static List<ListItem> ListTemplateTypeForEnum()
{
List<ListItem> list = new List<ListItem>();
foreach (int i in Enum.GetValues(typeof(SPListTemplateType)))
{
ListItem listitem = new ListItem(Enum.GetName(typeof(SPListTemplateType), i), i.ToString());
list.Add(listitem);
}
return list;
}
/// <summary>
/// 转换SPListTemplateType枚举
/// </summary>
/// <param name="Entype"></param>
/// <returns></returns>
public static SPListTemplateType GetListTemplateTypeForList(string Entype)
{
return (SPListTemplateType)Enum.Parse(typeof(SPListTemplateType), Entype, true);
}
/// <summary>
/// 取网站集内容类型集合
/// </summary>
/// <param name="strSite">站点地址</param>
/// <param name="strWeb">网站目录</param>
/// <returns></returns>
public static SPContentTypeCollection GetWebContentTypes(string strSite, string strWeb)
{
try
{
using (SPSite site = new SPSite(strSite))
{
site.AllowUnsafeUpdates = true;
using (SPWeb web = site.OpenWeb(strWeb))
{
web.AllowUnsafeUpdates = true;
return web.ContentTypes;
}
}
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("Foresee$Sharepoint", "取网站集内容类型集合" + ex.Message);
throw ex;
}
}
/// <summary>
/// 取网站集列表集合
/// </summary>
/// <param name="strSite">站点地址</param>
/// <param name="strWeb">网站目录</param>
/// <returns>SPListCollection</returns>
public static SPListCollection GetWebLists(string strSite, string strWeb)
{
try
{
using (SPSite site = new SPSite(strSite))
{
site.AllowUnsafeUpdates = true;
using (SPWeb web = site.OpenWeb(strWeb))
{
web.AllowUnsafeUpdates = true;
return web.Lists;
}
}
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("Foresee$Sharepoint", "取网站集列表集合" + ex.Message);
throw ex;
}
}
/// <summary>
/// 取list的GUID
/// </summary>
/// <param name="strSite">站点地址</param>
/// <param name="strWeb">网站目录</param>
/// <param name="strList">列表</param>
/// <returns>Guid</returns>
public static Guid GetListID(string strSite, string strWeb,string strList)
{
try
{
using (SPSite site = new SPSite(strSite))
{
site.AllowUnsafeUpdates = true;
using (SPWeb web = site.OpenWeb(strWeb))
{
web.AllowUnsafeUpdates = true;
SPListCollection lists = web.Lists;
SPList list = lists[strList];
return list.ID;
}
}
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("Foresee$Sharepoint", "取list的GUID" + ex.Message);
throw ex;
}
}
/// <summary>
/// 取View的GUID
/// </summary>
/// <param name="strSite">站点地址</param>
/// <param name="strWeb">网站目录</param>
/// <param name="strList">列表</param>
/// <returns>Guid</returns>
public static Guid GetViewID(string strSite, string strWeb, string strList)
{
try
{
using (SPSite site = new SPSite(strSite))
{
site.AllowUnsafeUpdates = true;
using (SPWeb web = site.OpenWeb(strWeb))
{
web.AllowUnsafeUpdates = true;
SPListCollection lists = web.Lists;
SPList list = lists[strList];
return list.DefaultView.ID;
}
}
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("Foresee$Sharepoint", "取View的GUID" + ex.Message);
throw ex;
}
}
/// <summary>
/// 删除列表
/// </summary>
/// <param name="strSite">站点地址</param>
/// <param name="strWeb">网站目录</param>
/// <param name="strList">列表名</param>
public static void DeleteListByName(string strSite, string strWeb, string strList)
{
try
{
using (SPSite site = new SPSite(strSite))
{
site.AllowUnsafeUpdates = true;
using (SPWeb web = site.OpenWeb(strWeb))
{
web.AllowUnsafeUpdates = true;
SPListCollection lists = web.Lists;
if (lists[strList] != null)
{
SPList list = lists[strList];
System.Guid gid = list.ID;
lists.Delete(gid);
}
}
}
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("Foresee$Sharepoint", "删除列表[" + strList + "]" + ex.Message);
throw ex;
}
}
/// <summary>
/// 删除列表ByGUID
/// </summary>
/// <param name="strSite">站点地址</param>
/// <param name="strWeb">网站目录</param>
/// <param name="strGUID">GUID</param>
public static void DeleteListByGUID(string strSite, string strWeb, string strGUID)
{
try
{
using (SPSite site = new SPSite(strSite))
{
site.AllowUnsafeUpdates = true;
using (SPWeb web = site.OpenWeb(strWeb))
{
web.AllowUnsafeUpdates = true;
SPListCollection lists = web.Lists;
if (lists[strGUID] != null)
{
SPList list = lists[strGUID];
System.Guid gid = list.ID;
lists.Delete(gid);
}
}
}
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("Foresee$Sharepoint", "删除列表ByGUID" + ex.Message);
throw ex;
}
}
/// <summary>
/// 取列表内容类型集合
/// </summary>
/// <param name="strSite">站点地址</param>
/// <param name="strWeb">网站目录</param>
/// <param name="strList">列表名</param>
/// <returns>SPContentTypeCollection</returns>
public static SPContentTypeCollection GetListContentTypes(string strSite, string strWeb, string strList)
{
try
{
using (SPSite site = new SPSite(strSite))
{
site.AllowUnsafeUpdates = true;
using (SPWeb web = site.OpenWeb(strWeb))
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists[strList];
return list.ContentTypes;
}
}
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("Foresee$Sharepoint", "取列表内容类型集合[" + strList+"]" + ex.Message);
throw ex;
}
}
/// <summary>
/// 取网站列表集转换为DataTable
/// </summary>
/// <param name="strSite">站点地址</param>
/// <param name="strWeb">网站目录</param>
/// <returns>DataTable</returns>
public static DataTable ListToDataTable(string strSite, string strWeb)
{
try
{
using (SPSite site = new SPSite(strSite))
{
site.AllowUnsafeUpdates = true;
using (SPWeb web = site.OpenWeb(strWeb))
{
web.AllowUnsafeUpdates = true;
SPListCollection lists = web.Lists;
DataTable dt = new DataTable();
dt.Columns.Add("GUID");
dt.Columns.Add("名称");
dt.Columns.Add("修改时间");
dt.Columns.Add("修改者");
dt.Columns.Add("目录数");
for (int i = 0; i < lists.Count; i++)
{
DataRow dr = dt.NewRow();
dr["GUID"] = lists[i].ID;
dr["名称"] = lists[i].Title;
dr["修改时间"] = lists[i].Created.ToString();
dr["修改者"] = lists[i].Author;
dr["目录数"] = lists[i].Folders.Count.ToString();
dt.Rows.Add(dr);
}
return dt;
}
}
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("Foresee$Sharepoint", "列表转换为DataTable" + ex.Message);
throw ex;
}
}
}
}
目前维护的开源产品:https://gitee.com/475660