0

On button click I am saving few values and the user who performed the action by using SPContext.Current.Web.CurrentUser in the list

But SPContext.Current.Web.CurrentUser is not returning the correct user who is currently logged in. Some times it is storing the other user.

My web-part have HTML form with server side button, this form is shared with whole department when user fill few text-boxes and click on button, ideally it should store the current user name, but some times record saved with different username.

protected void btnApprove_Click(object sender, EventArgs e)
{
       using (SPSite siteCol = new SPSite(SPContext.Current.Web.Url))
       {
                using (SPWeb web = siteCol.OpenWeb())
                {
                    SPList list = web.Lists["LISTNAME"];
                    SPListItem item = list.GetItemById(Convert.ToInt32(_paramReqId));

                    if (item != null)
                    {

                            item["Approver"] = SPContext.Current.Web.CurrentUser; 
                            item["ApproverTimeStamp"] = DateTime.Now; 
                            item["ApproverOutCome"] = "Approved";
                            item["ApproverComments"] = txtAreaApproverComments.Value;
                            item.Update();
                    }
                }
       }
}

EDIT: Item Updated ER code

public override void ItemUpdated(SPItemEventProperties properties)
    {
        base.ItemUpdated(properties);

        string requestUrl = string.Empty;
        string body = string.Empty;
        SPFieldUserValue Stage1Approver;
        SPFieldUserValue Stage2Approver;

            var context = SPContext.GetContext(properties.Web);

            string requestId = Convert.ToString(properties.ListItem["ID"]);
            SPUser user = new SPFieldUserValue(properties.Web, properties.ListItem["Author"].ToString()).User;
            string email = user.Email;
            string authorName = user.Name;

            string stage1outcome = Convert.ToString(properties.ListItem["Approver1OutCome"]);
            string stage1approverComments = Convert.ToString(properties.ListItem["Approver1ApproverComments"]);

            string stage2outcome = Convert.ToString(properties.ListItem["Approver2OutCome"]);
            string stage2approverComments = Convert.ToString(properties.ListItem["Approver2ApproverComments"]);

            Stage1Approver = new SPFieldUserValue(properties.Web, properties.ListItem["Approver1Approver"].ToString()); 
            Stage2Approver = new SPFieldUserValue(properties.Web, properties.ListItem["Approver2Approver"].ToString()); 


            var company = Convert.ToString(properties.ListItem["Company"]);
            var empName = Convert.ToString(properties.ListItem["EmployeeName"]) != "" ? Convert.ToString(properties.ListItem["EmployeeName"]) : authorName;
            var empCode = Convert.ToString(properties.ListItem["EmployeeCode"]);

            if (stage1outcome == Approved && stage2outcome == "")
            {
                StoreAssignedToUser(Stage2Approver, requestId, context);
            } 
            else if (stage1outcome == Approved && stage2outcome == Approved) 
            {
                CloseRequest(requestId, context)
            } 
        }

    public void StoreAssignedToUser(SPFieldUserValue userfieldvalue, string requestId, SPContext context)
    {
        SPSecurity.RunWithElevatedPrivileges(() =>
        {
            using (SPSite site = new SPSite(context.Web.Url.ToString()))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["LISTNAME"];
                    SPListItem item = list.GetItemById(Convert.ToInt32(requestId));

                    if (item != null)
                    {
                        item["AssignedTo"] = userfieldvalue;

                        EventFiring eventFiring = new EventFiring();

                        eventFiring.DisableHandleEventFiring();

                        item.Update();

                        eventFiring.EnableHandleEventFiring();
                    }
                }
            }
        });
    }

    public void CloseRequest(string reqId, SPContext context)
    {

            SPSecurity.RunWithElevatedPrivileges(() =>
            {
                using (SPSite site = new SPSite(context.Web.Url.ToString()))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists["LISTNAME"];
                        SPListItem item = list.GetItemById(Convert.ToInt32(reqId));

                        if (item != null)
                        {
                            item["RequestStatus"] = "CLOSED";

                            EventFiring eventFiring = new EventFiring();

                            eventFiring.DisableHandleEventFiring();

                            item.Update();

                            eventFiring.EnableHandleEventFiring();
                        }
                    }
                }
            });

    }

Is there any solid way to do this?

  • Why do you re-open SPSite/SPWeb (using…) objects instead of using the current/contextual SPWeb all the way? – Evariste May 15 at 5:53
  • Each time I've seen such a weird behavior is when there was additionnal code somewhere, like an event receiver. – Evariste May 15 at 5:57
  • Yes, after saving record. I am triggering Item Updated event. – Rishi Jagati May 15 at 6:08
  • Would you share the code of the Updated ER? – Evariste May 15 at 6:10
  • @Evariste added ER code. – Rishi Jagati May 15 at 8:54
-1

Can you please check with HardCodeed URL in

using (SPSite siteCol = new SPSite(**SPContext.Current.Web.Url**))

Thanks, Ani

  • I tried, but no help.out of 10 requests it happens once or twice. – Rishi Jagati May 15 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.