2011-06-28 28 views
0

我想從C#訪問用戶箱裏,這樣我可以通過所有在他箱裏的物品的循環和更新工作流狀態的項目的每個版本。編程訪問Sitecore的用戶箱裏

我使用Sitecore的6.2.0(修訂版101105)

感謝您的關注!

回答

4

編輯:下面的代碼需要在主上下文下運行 - 「從sitecore admin內部」。如果您是從Web上下文(網站上的頁面)運行它並且Context.Database是「web」,則需要使用Factory.GetDatabase(「master」)替換Sitecore.Context.Database的所有實例。


首先,你需要把所有的工作流程

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

隨後的foreach的工作流程,你需要把所有的狀態:

workflow.GetStates(); 

然後每個狀態,你可以得到所有的項目

var itemDataUris = workflow.GetItems(state); 

GetItems返回dataUris 列表所以,如果你正在使用有問題的用戶登錄後,您可以做

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 
} 

如果正在運行作爲管理員,並希望得到該項目另一位用戶,您需要將上述代碼包裝在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(); 

希望對您有所幫助!

+0

確保你不是試圖運行在網絡背景下,這個代碼...它不會工作。 – Bryan

+0

謝謝Brian。我會修改答案,以確保它說明。 – marto

+0

marto,非常感謝你爲你詳細的解釋。這正是我需要的! – DougCouto

2

不知道在您的代碼運行,或多久的情況下,但你可以利用從用戶箱裏的RSS源?

+0

不錯的選擇的API +1 – Younes