我正在自定義一個名爲「Quality Documents」的SharePoint文檔庫,以便在將新文檔添加到該庫時,生成一個隨機唯一的編號並將其應用於名爲「文件號碼」。我編碼下面的功能,但它不起作用。任何人都可以看到什麼可能是問題?沒有任何反應,沒有錯誤,頁面正常工作,但沒有生成文檔編號。有什麼建議麼?在SharePoint文檔庫中生成唯一的隨機字符串
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
namespace QualityDocHandler
{
class DocumentHandler : SPItemEventReceiver
{
/// <summary>
/// Generates a random string with the given length
/// </summary>
/// <param name="size">Size of the string</param>
/// <returns>Random string</returns>
private string RandomString(int size)
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
return builder.ToString();
}
private string createDocNum(SPItemEventProperties properties)
{
int newRnd = 0;
do
{
// set static department
string dept = "QUA";
// set date without separators
string dateString = DateTime.Today.ToString("ddMMyyyy");
// get 1st random string
string Rand1 = RandomString(4);
// get 1st random string
string Rand2 = RandomString(4);
// creat full document number
string docNum = dept + "-" + dateString + "-" + Rand1 + "-" + Rand2;
using (SPWeb oWeb = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl))
{
SPList oList = oWeb.Lists["Quality Documents"];
//create query
SPQuery oQuery = new SPQuery();
//configure the query //
oQuery.Query = "<Where><Eq><FieldRef Name='Document_x0020_Number' /><Value Type='Text'>" + docNum + "</Value></Eq></Where>";
//get the collection of items in the list
SPListItemCollection oItems = oList.GetItems(oQuery);
if (oItems.Count > 0)
{
newRnd = 0;
}
else
{
newRnd = 1;
}
}
return docNum;
}
while (newRnd < 1);
}
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
}
public override void ItemAdding(SPItemEventProperties properties)
{
string documentNum = createDocNum(properties);
using (SPWeb oWeb = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl))
{
SPListItem listItem = properties.ListItem;
properties.AfterProperties["Document_x0020_Number"] = documentNum;
listItem.Update();
oWeb.Update();
}
base.ItemAdding(properties);
}
public override void ItemUpdated(SPItemEventProperties properties)
{
base.ItemUpdated(properties);
}
public override void ItemUpdating(SPItemEventProperties properties)
{
base.ItemUpdating(properties);
}
}
}
當你將這段代碼打包時會發生什麼?你知道哪個線斷了嗎? – Colin 2009-07-10 17:27:55
Jeez,那是一些低調的Engilsh在那裏,對不起.. – Colin 2009-07-10 17:29:10