开发者

Programmatically access Sitecore user's workbox

开发者 https://www.devze.com 2023-03-16 11:35 出处:网络
I would like to access a user\'s workbox from C# so that I can loop through all of the items in his workbox and update the workflow state for each version of the items.

I would like to access a user's workbox from C# so that I can loop through all of the items in his workbox and update the workflow state for each version of the items.

I'm using Sitecore 6.2.0 (rev.开发者_StackOverflow中文版 101105)

Thank you for your attention!


EDIT: The following code needs to be run under under a master context - "From inside the sitecore admin". If you are running it from a web context (a page on the site) and Context.Database is "web" then you need to replace all instances of Sitecore.Context.Database with Factory.GetDatabase("master")


First you need to get all the workflows

Sitecore.Context.Database.WorkflowProvider.GetWorkflows();

Then foreach workflow you need to get all the states:

workflow.GetStates();

Then for each state you can get all the items

var itemDataUris = workflow.GetItems(state);

GetItems returns a list of dataUris So if you are currently logged in with the user in question you can just do

foreach (var dataUri in itemDataUris)
{
    var item = Context.Database.GetItem(dataUri);
    //check if the item is null (if null the user doesn't have access to it
    if (item != null)
       usersWorkflowItems.Add(item); //Here you also need to somehow add which state the item is in
}

If are running as an admin and want to get the item for another user you need to wrap the above code in a UserSwitcher

Sitecore.Security.Accounts.UserSwitcher.Enter(Sitecore.Security.Accounts.User.FromName("sitecore\User", true));
foreach (var dataUri in itemDataUris)
    {
        var item = Context.Database.GetItem(dataUri);
        //check if the item is null (if null the user doesn't have access to it
        if (item != null)
           usersWorkflowItems.Add(item); //Here you also need to somehow add which state the item is in
    }
Sitecore.Security.Accounts.UserSwitcher.Exit();

Hope that helps!


Not sure the context of where your code is running, or how often, but could you utilize the RSS feed from the user's workbox?

0

精彩评论

暂无评论...
验证码 换一张
取 消