none
Getting current logged in user in VS workflow

    질문

  • Hi

    I'm having issues getting the current logged in user. this is the code I've used in my workflow (VS):
    the 'SPControl.GetContextWeb(currentContext)' is undelined in red

    Error 1 The best overloaded method match for 'Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(System.Web.HttpContext)' has some invalid arguments 

       SPContext currentContext = SPContext.Current;
                string uName = string.Empty;
    
                if (currentContext != null && currentContext.Web.CurrentUser != null)
                {
                    SPWeb web = SPControl.GetContextWeb(currentContext);
                    uName = web.CurrentUser.LoginName;
                }
    
                else
                {
                    uName = System.Web.HttpContext.Current.User.Identity.Name;
                }

    2012년 6월 15일 금요일 오후 1:18

답변

  • Hi orange juice jones,

    You can get the person who complete the task in workflow by the code below:

            private void completeTask1_MethodInvoking(object sender, EventArgs e)
            {
                SPListItem item = workflowProperties.TaskList.GetItemById(TaskProperties1.TaskItemId);
                SPUser user =(SPUser) item["Modified By"];
            }

    Thanks,
    Simon

    Simon Huang

    TechNet Community Support

    • 답변으로 표시됨 Shimin Huang 2012년 6월 21일 목요일 오전 7:18
    2012년 6월 18일 월요일 오전 7:43

모든 응답

  • Hi

    SPWeb web = SPControl.GetContextWeb(this.Context);
    string uName = web.CurrentUser.LoginName;
    I may be wrong here , but I don't think you can have a context without a logged in user, that's why I omitted the if test.

    Kind Regards Bjoern
    Blog

    2012년 6월 15일 금요일 오후 1:44
    중재자
  • Hi

    I've tried your code and this time the word Context is underlined.

    Error 1 does not contain a definition for 'Context' and no extension method 'Context' accepting a first argument of type

    could be found (are you missing a using directive or an assembly reference?) 

                SPContext currentContext = SPContext.Current;
                string uName = string.Empty;
    
                SPWeb web = SPControl.GetContextWeb(this.Context);
                uName = web.CurrentUser.LoginName;

    2012년 6월 15일 금요일 오후 1:49
  • That didn't look correct, try:   

    SPWeb web = SPControl.GetContextWeb(HttpContext.Current);

    + you may have to add a

    using System.Web; 


    Kind Regards Bjoern
    Blog


    2012년 6월 15일 금요일 오후 2:03
    중재자
  • hi

    Now HttpContext is underlined. I've added using System.Web

    Error 1 The name 'HttpContext' does not exist in the current context 

    Any ideas ?

    2012년 6월 15일 금요일 오후 2:20
  • You said in a workflow? There is no httpcontext in a workflow. After all, the workflow could be executing long after everyone has logged off. There is an originatoruser property of workflowproperties:

    http://www.sharepointkings.com/2008/08/how-to-get-currently-logged-in-user-in.html


    Mike G.

    2012년 6월 15일 금요일 오후 2:52
  • Hi

    I just tried the link above and it doesnt work either, I get the person who created the list item and not the person who completed the task.

    I've come to realise that there could be a number of people logged in at any one time. I want the person who completed the task in the workflow.

    Thanks

    2012년 6월 16일 토요일 오전 9:32
  • Hi orange juice jones,

    You can get the person who complete the task in workflow by the code below:

            private void completeTask1_MethodInvoking(object sender, EventArgs e)
            {
                SPListItem item = workflowProperties.TaskList.GetItemById(TaskProperties1.TaskItemId);
                SPUser user =(SPUser) item["Modified By"];
            }

    Thanks,
    Simon

    Simon Huang

    TechNet Community Support

    • 답변으로 표시됨 Shimin Huang 2012년 6월 21일 목요일 오전 7:18
    2012년 6월 18일 월요일 오전 7:43