编写高质量高效率的SharePoint应用程序

资源管理方面的一个陷阱就是说
SPSite和SPWeb

SPSite site=new SPSite("http://localhost");
SPWeb web=site.OpenWeb();

相信这两句是每一个写sharepoint开发的人都会写的,可是就在这两句中就有一个大陷阱,是什么呢,就是这两个对象是非托管的对象,是需要明确释放的,释放的两种方式为:
1、using
2、try。。。catch。。。finally。。。
但是呢,有两个意外,那就是

SPSite site= SPContext.Current.Site;
SPWeb web=SPContext.Current.Web;

从SPContext,也就是sharepoint的上下文中获取的这两个对象是托管对象,可以不用显示释放。

二、低效的删除陷阱

SPList list;
foreach(SPListItem item in list.Items)
{
       list.Items.DeleteItemById();
}

这一段循环删除lsit中的items,效率很低的,在演示中,删除一条大概要半秒多,大家有兴趣可以尝试一下的。

那么什么是高效的删除操作呢,就是用

SPWeb web;
web.ProcessBatchData();

spweb中的批处理方法,效率要高300%多呢,详细代码如下:

Deleting a large number of items from a list in SharePoint 
From :http://blog.thekid.me.uk/archive/2007/02/24/deleting-a-considerable-number-of-items-from-a-list-in-sharepoint.aspx

Recently the question was asked in the newsgroups about deleting a large number of items from SharePoint (WSS) in the fastest way. I had, in one if my projects, needed to remove a large number of item from SharePoint and the best way I found was to use 'ProcessBatchData' as it avoided the API and was considerably faster. 

Here is some example code which will remove items from a SharePoint list. If you do this I would remember about the Recycle Bin and the effect deleting many items will have in the futureit maybe worth adding some code which also removes the items from the recycle bin once it has finished. 

StringBuilder sbDelete = new StringBuilder();
sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");

foreach (SPListItem item in CurrentList.Items)
{
    sbDelete.Append("<Method>");
    sbDelete.Append("<SetList Scope=\"Request\">" + CurrentList.ID + "</SetList>");
    sbDelete.Append("<SetVar Name=\"ID\">" + Convert.ToString(item.ID) + "</SetVar>");
    sbDelete.Append("<SetVar Name=\"Cmd\">Delete</SetVar>");
    sbDelete.Append("</Method>");
}

sbDelete.Append("</Batch>");

try
{
    SPContext.Current.Site.RootWeb.ProcessBatchData(sbDelete.ToString());
}
catch (Exception ex)
{
    Console.WriteLine("Delete failed: " + ex.Message);
    throw;
} 

I took me some time to re-find this code and so I have posted it here more for my benefit, but if anyone finds it useful then great. 

三、低效的查询

SPList list;
foreach(SPListItem item in list.Items)
{
    if(item["name"]=="virus")
    {
       Console.WriteLine("yes");
    }
}

像这样的循环查找,除非你的列表中只有很少的记录,要不就使用SPQuery,上面的做法会导致返回全部的对象,所有效率很低,有时候效率低,不是因为sharepoint本身的问题,而是我们选择了错误的方法。
SPQuery的最大缺陷就是不支持联合查询,也就是sql中的join,不知道下一个版本会不会实现这个很好用的功能呢。

四、得到list的item的数量
可能我们都习惯了

list.Items.Count

这样的效率也是非常低下的,因为他会返回alllitem,这是设计问题,但是微软提供了

list.ItemCount;

这个属性同样是返回count,但是却没有了上面的问题,建议大家使用。


还有啊,这个版本还有一个2000限制,就是说一个应用最好不要超过2000个网站,一个网站不要最好超过2000个列表,一个列表下面最好不要超过2000个item,超过之后性能会急剧下降,但是不是致命的,超过2000,微软建议建立文件夹来分类存放,当前版本的列表也支持文件夹了吗。

另外的一节课是候钟雷和杜伟的自定义列的开发,主要的意义还在于扩展了我们的开发思路,列不仅仅是一个单行文本或者日期输入之类的东西了,而是可以看做一个对象,然后这个对象又是一个复杂的输入。
比如说在场地订阅功能中,自定义一个冲突检测列,就是一个验证函数,用来验证场地的订阅日期和已经存在的时间有无冲突。还可以表现一个主子表的内容,就是说可以是一个SPGridView控件,来显示详细表的内容,然后还可以编辑。微软的权限可以控制到条目级,但是列可以定义是否可以,是否可以编辑,估计下一版会丰富到栏目级吧;

原文地址:https://www.cnblogs.com/masahiro/p/10128976.html