2

我有这个事件接收器 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 { }
        }
    }
}
4

3 回答 3

2

不要这样做:SPUser privilegedAccount = properties.Web.AllUsers[@"SHAREPOINT\SYSTEM"]; 阅读使用SPSecurity.RunWithElevatedPrivileges. 请参阅此处的 MSDN 文档

也不要使用 using (SPSite... 并且在 using 块内您尝试通过该网络获取网络SPContext.Current- 该网络将不再被提升。

正确的方法是沿着这些思路(我没有尝试这个,所以它只是为了让你知道你要去哪里):

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-21T09:55:45.477 回答
1

我必须得到这样的文件夹名称:

string foldername = Convert.ToString(properties.AfterProperties["Title"]);
于 2012-09-26T20:40:07.110 回答
0

您是否尝试调试它?尝试调试并告诉我们您遇到了什么错误。但是在您调试之前首先使用共享点管理器来查看您的事件接收器是否正确连接。

如果您不知道如何调试共享点事件接收器,请参阅此

于 2012-09-21T01:14:13.757 回答