This is a public Forum  publicRSS

Topic

    Ammar Aldaffaie
    Current Logged in user ID in AddinAnswered
    Topic posted June 16, 2017 by Ammar AldaffaieApprentice, last edited June 16, 2017 
    108 Views, 11 Comments
    Title:
    Current Logged in user ID in Addin
    Content:

    Hi,

    I have a Global Ribbon Addin that is doing update to a custom field for a staff account, it is working perfect when I pass the Account ID in the code but what I need is to pass the logged in account ID and this is what I do not know how to do.

    So my question is can you please help me to find the current logged in staff account ID and use it in Addin?

    Basically what I need is to make the update to the logged in staff account.

    I'm using C#

    Thank you so much

    Version:
    17.2

    Best Answer

    Anurag Mittal

            public bool Initialize(IGlobalContext context)
            {
                int accointId= context.AccountId;
                return true;
            }

    Answer

     

    • Anurag Mittal

              public bool Initialize(IGlobalContext context)
              {
                  int accointId= context.AccountId;
                  return true;
              }

    • Scott Harwell

      The GlobalContext object has a property AccountId, which is the account ID of the logged in user.  You can use that ID and the SOAP API to make changes to the account back to the cloud.

      EDIT: Looks like Anuraj hit submit first.  :)

    • Ammar Aldaffaie

      Thank you Anuraj for the quick respond and for the help this is what I needed.

      Scott, thank you so much :)

    • Ammar Aldaffaie

      What is the best way to pass this value to the click button?

      I tried this but did not work

      I set a property in the class and let the Initialize to set the value but I'm getting 0 when I try to use it in the click button?

      For example this is what I want to do and I like to display the ID

       

       public void Click()
              {


                  MessageBox.Show(currentAccountID.ToString());

              }

    • Scott Harwell

      Are you sure that you're setting the value from the GlobalContext to that property and doing it at the right time?  When you add a breakpoint to the initialize method, does the GlobalContext object have a value for the AccountId?  It always will, but that breakpoint, and subsequent breakpoints, should allow you to walk through the assignment of the field (assuming currentAccountID is a field or property) and the state of the class as the assignment happens.

      You might just assign the global context to a field so that you can reuse it and then you could just call GlobalContext.AccountId from your MessageBox call; one fewer points of failure.

      Any source code that you can share will make troubleshooting easier.

    • Ammar Aldaffaie

      Hi Scott,

      This is what I did

      In the class set the property 

      int currentLoggedIn { get; set;}

      in the public bool Initialize(IGlobalContext GlobalContext)
              {
                  int currentLoggedIn = GlobalContext.AccountId;
                  
                  return true;
              }

      In the button click

       public void Click()
              {
                  MessageBox.Show(currentLoggedIn.ToString());
              }

      I get 0 for the MessageBox and I don't know what I'm doing wrong

    • Ammar Aldaffaie

      This is what I'm trying to accomplish

      passing the logged in account ID to this 

       

                  Account newAccount = new Account();
                  ID accountID = new ID();
                  accountID.id = (I need the loggted in staff account ID here when the button clicked)
                  accountID.idSpecified = true;
                  newAccount.ID = accountID;

    • Scott Harwell

      If you attach VS to your OSvC instance and put a breakpoint on return true in your Initialize method, then can you see a value for the account ID in either the GlobalContext or this objects? 

    • Ammar Aldaffaie

      Yes, I can see the Account ID value in the Initialize but I'm not doing the right thing to pass it to the button or anywhere else outside the Initialize

    • Scott Harwell

      Your syntax in Initialize looks like you're setting a local property in the method, not the class, since you have the data type in front of the variable.

      int currentLoggedIn = -1; //or you can use int? and set to null
      ...
      public bool Initialize(IGlobalContext GlobalContext)
      {
          currentLoggedIn = GlobalContext.AccountId; //Should set the field on the class
          return true;
      }
      
    • Ammar Aldaffaie

      Scott you are awesome this is worked and I see what I was doing wrong. Cannot thank you enough sir.