好的,所以我有一个来自用户的请求,我想使用 和 事件接收器来实现它。
基本上,她想要的是一个用户可以上传文档的文档库。每个文档都有一个文件名,如下所示"UserA - 自定义.docx"。现在您可以想象用户可以上传许多具有相同名称的文档,因此我们要做的是自动对文件进行编号。因此,如果用户 A 上传第一个文档,SharePoint 将在名称后添加一个数字,因此该文件将称为"用户 A - 自定义 - 1.docx",然后他上传第二个文档,它将称为"用户 A - 自定义 - 2.docx"。
但是,现在如果用户 B 想要上传他的第一个文档,它必须编号为"用户 B - 自定义 - 1.docx",所以基本上如果它是一个新的文档,计数器需要重新启动,如果文档名称已经存在,则从最高数字继续。
因此,基本上SharePoint需要检查列表中是否存在当前文档的名称,以及它是否在其旁边添加了数字,但是该数字必须比最高文档大1,因此它只会增加。
有什么办法可以做到这一点吗?有什么想法吗?
这就是我到目前为止想出的,只需更改文件名,将"-xx"添加到文件名中,但这不起作用。
public override void ItemAdded(SPItemEventProperties properties)
{
SPFile spf = properties.ListItem.File;
string url = properties.AfterUrl;
int positionOfSlash = url.LastIndexOf("/");
string pathBeforeFileName = url.Substring(0, positionOfSlash);
string newFileName = createNewFileName(url.Substring(positionOfSlash));
string myNewUrl = pathBeforeFileName + newFileName;
DisableEventFiring();
spf.MoveTo(myNewUrl);
spf.Update();
EnableEventFiring();
}
static string createNewFileName(string oldFileName)
{
int positionOfPeriod = oldFileName.LastIndexOf(".");
string fileName = oldFileName.Substring(0, positionOfPeriod);
string fileExtension = oldFileName.Substring(positionOfPeriod);
string newFileName = fileName + "-xx" + fileExtension;
return newFileName;
}
这段代码哪里出错了?感谢您的任何帮助!
编辑:这是我在Visual Studio的控制台应用程序中使用的代码,用于将EventReceiver附加到文档库。
using (SPSite site = new SPSite("http://servername:port/subsite1/subsite2/"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["My Doc Library"];
SPEventReceiverDefinition def = list.EventReceivers.Add();
def.Assembly = "DocumentLibrary_ClassLib, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=611205b34d18f14d";
def.Class = "DocumentLibrary_ClassLib.EventReceiver";
def.Type = SPEventReceiverType.ItemAdded;
def.Update();
}
}
编辑#2:好吧,这样的事情怎么样?
//this will get just the name of the file without the extension and I will send that to the
//query builder which will count how many files there are with that name and return
int positionOfPeriod = oldFileName.LastIndexOf(".");
string tempFileName = oldFileName.Substring(0, positionOfPeriod);
SPQuery query = BuildArbitraryQuery(properties.List, "Name", tempFileName, true);
但是现在我真的不了解 BuildArbitraryQuery 中的查询,我该如何更改它以提供所需的行为?(对不起,如果这是一个完全的菜鸟问题,但我以前从未处理过 C# 和 EventReceivers)
- 在看了一段时间的BuildArbitraryQuery之后,我想我理解了一些,基本上我不需要改变任何东西?由于它接收文件名和列名作为参数,因此应该没问题吧?
此外,由于列表中的项目类似于 ClientA Request - 3.docx并且我将文件名发送到 BuildArbitraryQuery 将能够找到部分匹配而不是完全匹配。因此,例如,如果 BuildArbitraryQuery 收到的文件名是 ClientA Request.docx它是否能够找到来自该 ClientA 的所有其他请求?那么客户A请求 - 1.docx,客户A请求 - 2.docx都包含在计算中吗?
你去吧,测试了它,它可以工作。
/// <summary>
/// An item was added.
/// </summary>
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
SPListItem item = properties.ListItem;
if (item["Name"] == null)
return; //or better yet, log
string oldFileName = item["Name"].ToString();
SPQuery query = BuildArbitraryQuery(properties.List, "Created By", properties.UserDisplayName, true);
int count = properties.List.GetItems(query).Count;
int positionOfPeriod = oldFileName.LastIndexOf(".");
if (positionOfPeriod == -1)
{
fileName = oldFileName;
fileExtension = "";
}
else
{
fileName = oldFileName.Substring(0, positionOfPeriod);
fileExtension = oldFileName.Substring(positionOfPeriod);
}
string newFileName = fileName + "-xx" + count.ToString() + fileExtension;
item["Name"] = newFileName;
try
{
properties.Web.AllowUnsafeUpdates = true;
EventFiringEnabled = false;
item.Update();
}
finally
{
properties.Web.AllowUnsafeUpdates = false;
EventFiringEnabled = true;
}
}
/// <summary>
/// Builds an arbitrary SPQuery which filters by a single column value.
/// </summary>
/// <param name="list">The list you will run the query against.</param>
/// <param name="columnDisplayName">The Display Name of the column you want to filter.</param>
/// <param name="value">The value to filter against.</param>
/// <returns>A new SPQuery object ready to run against the list.</returns>
public static SPQuery BuildArbitraryQuery(SPList list, string columnDisplayName, string value, bool deepSearch)
{
if (list == null)
throw new ArgumentNullException("You cannot pass a null list to Helper.BuildArbitraryQuery.");
if (!list.Fields.ContainsField(columnDisplayName))
throw new ArgumentException("The SharePoint List "" + list.Title + "" does not contain the Field "" + columnDisplayName + "".");
string internalName = list.Fields[columnDisplayName].InternalName;
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name="" + internalName + ""/><Value Type="Text">" + value + "</Value></Eq></Where>";
if (deepSearch)
query.ViewAttributes += "Scope='RecursiveAll'";
return query;
}