我有这个事件接收器c#类,我正试图在Sharepoint站点上实现它。它没有起作用。我已经从visual studio 2010部署了它,在它构建正常之后。有没有人看到问题出在哪里?代码正确吗?或者问题出在SP上?谢谢。-这是新的代码
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
namespace EventReceiverCFolder.EventReceiver1
{
/// <summary>
/// List Item Events
/// </summary>
public class EventReceiver1 : SPItemEventReceiver
{
/// <summary>
/// An item is being added.
/// </summary>
public override void ItemAdded(SPItemEventProperties properties)
{
try
{
if (properties.ListTitle == "CF") // list where the item was added
{ // if item was added to this list then create a folder on - Dlib - list
UpdateFolder(properties);
}
}
catch (Exception ex)
{
properties.Status = SPEventReceiverStatus.CancelWithError;
properties.ErrorMessage = ex.Message;
properties.Cancel = true;
}
}
private void UpdateFolder(SPItemEventProperties properties)
{
string foldername = properties.ListItem["Title"].ToString();
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
//inside RunWithElevatedPriviliges I need to open a new site (an elevated site)
using (SPSite site = new SPSite(properties.Web.Site.ID))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists.TryGetList("DLib"); // this is doc Library where the new folder will be created
//note that we are creating a list item, not a folder - a folder IS a list item
SPListItem createdFolder = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, null);
if (createdFolder != null)
{
createdFolder["Name"] = foldername;
createdFolder.Update();
}
list.Update();
}
}
});
}
finally { }
}
}
}发布于 2012-09-27 04:40:07
我必须像这样获取文件夹名称:
string foldername = Convert.ToString(properties.AfterProperties["Title"]);发布于 2012-09-21 17:55:45
不要这样做:SPUser privilegedAccount = properties.Web.AllUsers[@"SHAREPOINT\SYSTEM"];阅读了使用SPSecurity.RunWithElevatedPrivileges的相关知识。请参阅MSDN documentation here。
也不要使用(SPSite...在using代码块中,您尝试通过SPContext.Current获取web -该web将不再被提升。
正确的方法是这样的(我没有尝试过,所以它只是给你一个想法):
private void UpdateFolder(SPItemEventProperties properties)
{
string foldername = properties.ListItem["Title"].ToString();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
//inside RunWithElevatedPriviliges I need to open a new site (an elevated site)
using (SPSite site = new SPSite(properties.Web.Site.ID))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists.TryGetList("ListTitle"); //is that really the list title?
//note that we are creating a list item, not a folder - a folder IS a list item
SSPListItem createdFolder = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, null);
if (newFolder != null)
{
createdFolder["Name"] = foldername;
createdFolder.Update();
}
list.Update();
}
}
});
}还要试着调试你的代码,设置断点等。
发布于 2012-09-21 09:14:14
你试过调试它吗?试着调试并告诉我们你得到的是什么错误。但在调试之前,请先使用sharepoint manager查看事件接收器是否已正确连接。
如果您不知道如何调试sharepoint,请使用see this
https://stackoverflow.com/questions/12522802
复制相似问题