将元素添加到特定用户的公告列表中 [英] Add elements to an an announcements list for a specific user

查看:79
本文介绍了将元素添加到特定用户的公告列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户请求,要根据另一个数据库中的某些数据来创建多个公告.大部分看起来很容易,但是新元素应该由输入数据中指定的用户(登录)创建. 我打算使用列表Web服务添加公告,但是我想避免使用模拟来获得创建用户权限.有没有办法在不使用模拟的情况下将正确的用户分配为创建者?

I have a customer request to create a number of announcements based on some data from another database. Most of it seems easy enough but the new elements should be created by the user (login) specified in the input data. I was planning to add the announcements using the list web services but I sure would like to avoid using impersonation in order to get the create user right. Is there a way to assign the correct user as the creator without using impersonation?

推荐答案

这可能不是您要找的答案,但是如果您在SharePoint服务器上的GAC中运行代码,则模拟非常容易.您不需要知道许多密码都无法识别的密码,因此我将继续假设这是您不想进行模拟的原因.这是这样做的方法.

This may not be the answer you are looking for, but impersonation is pretty easy if you have code running in the GAC on a SharePoint server. You don't need to know any password which many do not realize, so I'll continue assuming that this was the reason you did not want to do impersonation. Here's how to do it.

您可以使用用于SPSite的典型构造函数连接到SharePoint,并找到适当的SPUser对象.完成此操作后,您可以获取该SPUser的UserToken属性.然后,您将需要再次使用SPSite构造函数,但要使用提供SPUserToken的重载.然后,您在SharePoint中所做的任何事情都将通过模拟来完成.无需以提升的特权运行.

You can connect to SharePoint using the typical constructor you use for SPSite and find the appropriate SPUser object. Once you do that, you can get the UserToken property for that SPUser. Then you'll need to use the SPSite constructor again, but use the overload that provides the SPUserToken. Then anything you do in SharePoint will be done via impersonation. No need to run with elevated privileges.

好的,现在我已经说过了,我将尝试猜测代码.应该是这样的:

OK, now that I've said it in words, I'll try to guess at the code. It should be something like:

// Just determine the user token for a particular user
SPUserToken userToken = null;
using (SPSite tempSite = new SPSite("http://sharepointurl"))
{
    using (SPWeb tempWeb = tempSite.OpenWeb())
    {
        // I think this next line works, but I'm going from memory
        // I believe the user needs to have already logged into the site at least once
        SPUser user = tempWeb.AllUsers["username"];
        userToken = user.UserToken;
    }
}

// Now do whatever we want impersonating that user
using (SPSite site = new SPSite("http://sharepointurl", userToken))
{
    using (SPWeb web = site.OpenWeb())
    {
        // Do whatever you want here
    }
} 

这篇关于将元素添加到特定用户的公告列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆