locked
Get the unique session for the user RRS feed

  • Question

  • Hi.

    Here's my scenario, a user logs in to the sharepoint site and visits a document library, then I have this webpart that gets the username of the user and stores it in the database, however, my problem is that whenever the user refreshes the browser(press F5), another session ID is thrown to the user meaning, my code will again put the username to the database and it will now have 2 data/record in my database.

    What I want to do is that whenever a user visits a document library, the username will be stored in the database once, or until he logs out or after a certain amount of time. and if the user refreshes his browser(press F5) it will not count as a view/hit therefore, the username will not be stored to the database.

    I've been trying this with alot of if statements however I cant find a way to handle this. I think i really need your help again.

     

     

    Thursday, December 2, 2010 4:58 AM

Answers

  • This is how you create session variables

    session["VisitedToday"] : instead of "VisitedToday" there can be any name of the variable you want to give...

     

    session["VisitedToday"]="Some Value" will store the value in the "VisitedToday" variable. it will create new variable if this is not already present else it will store in the same variable..

     

    in your webpart you can user like this.Page.Session["Variable Name"]

     

     

     

     

    • Marked as answer by Ton-Ton Keaton Wednesday, December 8, 2010 5:52 AM
    Wednesday, December 8, 2010 2:31 AM
  • Hi JayGeeBee,

     

                    Please forgive me, shyjumohan. I think you misunderstand me.

    According to MSDN the reason/solution is:
    “When using cookie-based session state, ASP.NET does not allocate storage for session data until the Session object is used. As a result, a new session ID is generated for each page request until the session object is accessed. If your application requires a static session ID for the entire session, you can either implement the Session_Start method in the application’s Global.asax file and store data in the Session object to fix the session ID, or you can use code in another part of your application to explicitly store data in the Session object.”

    And here is code snippet for your reference.

                                if (Session["Access"] == null)

                      {

                            Session.Add("Access", true);

                }

    Hope this could help you.

     

    Best Regards,

    Porter Wang

    • Marked as answer by Ton-Ton Keaton Wednesday, December 8, 2010 5:52 AM
    Wednesday, December 8, 2010 3:13 AM

All replies

  • Hi,

     

    You can try UrlReferer to find out the page refresh..If UrlReferer has the same url as of the current url then its a page referesh else its not

     

    this

     

    .Page.Request.UrlReferrer

     

    Regards,

    VJ

    Thursday, December 2, 2010 5:52 AM
  • Hi Vijander,

    I tried it out but unfortunately its not working. Here's what my code do,

    string url = HttpContext.Current.Request.Url.ToString();

    if (Page.Request.UrlReferrer.ToString() != url)

    {

    <put username to the database code>

    }

     

    I debugged my code and when i refresh my page the UrlReferrer shows my homepage (http://server/sites/test)

    btw, i have put my web part in a document library so I refresh my document library page.

    Thursday, December 2, 2010 6:38 AM
  • Have you tried creating session variable...

     

    You can enable session variables in sharepoint site from web.config

    <pages enableSessionState="true" 

     

    Regards,

    VJ

    Thursday, December 2, 2010 7:08 AM
  •  

    I think you can do this with hidden fields

    Put one hidden field on your webpart/ master page where ever you are convienent, once the user is logged in to the site store that value into database then store the value into hidden field

    just put if condition to check whether the hidded field contains value or not, if it contains value don't store it into database otherwise store into database

    if (hdnField.value != ""){

    //Don't store into database

    }

    else{

    //Store into the database

    }

    Please let me know if you need more info

     


    Raj.

    CodePlex : http://pagelayoutgenerator.codeplex.com

    Blog : http://mspbox.blogspot.com

     

    Thursday, December 2, 2010 7:36 AM
  • I have tried creating a HttpSessionState SessionID, however, when the users refreshes the page, the sessionID also changes.

    Thursday, December 2, 2010 7:42 AM
  • I think youe environment is using inmemory session state so, the session id wil change for each and every req

    I think just try below approach storing wiht username in hidden field

    remember yuo have store username in hidded field after user name is stored in to the database

    Put one hidden field on your webpart/ master page where ever you are convienent, once the user is logged in to the site store that value into database then store the value into hidden field

    just put if condition to check whether the hidded field contains value or not, if it contains value don't store it into database otherwise store into database

    if (hdnField.value != ""){

    //Don't store into database

    }

    else{

    //Store into the database

    }

     


    Raj. CodePlex : http://pagelayoutgenerator.codeplex.com Blog : http://mspbox.blogspot.com
    Thursday, December 2, 2010 7:47 AM
  • How will I clear the content of the hidden field?

    I want to clear the field if the user logs out.

    Thursday, December 2, 2010 8:22 AM
  • In the page load of the webpart check identity of currenlty logged in user using httpcontext.current.user.name, if  this retrns empty then clear the hidden field or if not dont clear

    I think same approach you can do using cookies instead of storing value in hidden field store that in cookie

    Let me know if you need more info

     


    Raj. CodePlex : http://pagelayoutgenerator.codeplex.com Blog : http://mspbox.blogspot.com
    Thursday, December 2, 2010 9:11 AM
  • I can't make it work

    here's my code:

    OnLoad() {

    Username();

    }

    Username(){

    textbox hidden = new textbox();

    hidden = ""

    if(hidden != "")

    {

    //do not store username

    }

    else {

    //store username to database

    hidden.text = username;

    }

     

    it erases the value of the hidden field during the first visit.


    ----------------------- Sharepoint Newbie
    Friday, December 3, 2010 12:20 AM
  • I see two problems over here

    1. functionality point of view the textbox is different and hidden field is different , so you should use HiddenField control not textbox

    2. Don't create object inside page load event, for each and every time I mean whenevr page refreshes, it will create new object ,so object will be always blank


    Raj. CodePlex : http://pagelayoutgenerator.codeplex.com Blog : http://mspbox.blogspot.com
    Friday, December 3, 2010 7:54 AM
  • oh.. so you mean the HiddenField control. i thought i will be creating a textbox and will set it to visible=false. LOL.

    if i will not create it at the page load. where? im really new to programming and sharepoint ><


    ----------------------- Sharepoint Newbie
    Friday, December 3, 2010 9:06 AM
  • Class UserManger

    {

    HiddenField  hidden = null;

    OnLoad() {

    Username();

    }

    Username(){

    if(hidden != null)

    {

    //do not store username

    }

    else {

    //store username to database

    hidden  = new HiddenField();

    hidden.text = username;

    }


    Raj. CodePlex : http://pagelayoutgenerator.codeplex.com Blog : http://mspbox.blogspot.com
    Friday, December 3, 2010 11:14 AM
  • Hi,

    I debugged the code and the hidden stays null when I refresh my page. :(

    so two user\username was in the database :(


    ----------------------- Sharepoint Newbie
    Sunday, December 5, 2010 11:48 PM
  • Hi JayGeeBee,

                    For your issue, I think you’d better fix it using session. As you mentioned that sessionID will change whenever user refreshes the browser, that will happen surely if you have not saved anything to the Session yet.

                    So add something to it and then SessionID will not change on subsequent requests for the life of the Session.

                   

    Best Regards,

    Porter Wang

    Tuesday, December 7, 2010 5:52 AM
  • Hi JayGeeBee,

                    For your issue, I think you’d better fix it using session. As you mentioned that sessionID will change whenever user refreshes the browser, that will happen surely if you have not saved anything to the Session yet.

                    So add something to it and then SessionID will not change on subsequent requests for the life of the Session.

                   

    Best Regards,

    Porter Wang


    Hi, Porter Wang.

    Thanks! But how will I add something to the session?


    ----------------------- Sharepoint Newbie
    Tuesday, December 7, 2010 11:36 PM
  • Try something like

    if (session["VisitedToday"] == null)

    push the user to DB

    else

    session["VisitedToday"] == "Yes"

    Wednesday, December 8, 2010 12:55 AM
  • Try something like

    if (session["VisitedToday"] == null)

    push the user to DB

    else

    session["VisitedToday"] == "Yes"


    Thanks, but where did you get the "VisitedToday" index? sorry but im really new to this. i'm a bit disappointed that i can't get through this simple programming. thanks for all your help.
    ----------------------- Sharepoint Newbie
    Wednesday, December 8, 2010 2:15 AM
  • This is how you create session variables

    session["VisitedToday"] : instead of "VisitedToday" there can be any name of the variable you want to give...

     

    session["VisitedToday"]="Some Value" will store the value in the "VisitedToday" variable. it will create new variable if this is not already present else it will store in the same variable..

     

    in your webpart you can user like this.Page.Session["Variable Name"]

     

     

     

     

    • Marked as answer by Ton-Ton Keaton Wednesday, December 8, 2010 5:52 AM
    Wednesday, December 8, 2010 2:31 AM
  • Hi JayGeeBee,

     

                    Please forgive me, shyjumohan. I think you misunderstand me.

    According to MSDN the reason/solution is:
    “When using cookie-based session state, ASP.NET does not allocate storage for session data until the Session object is used. As a result, a new session ID is generated for each page request until the session object is accessed. If your application requires a static session ID for the entire session, you can either implement the Session_Start method in the application’s Global.asax file and store data in the Session object to fix the session ID, or you can use code in another part of your application to explicitly store data in the Session object.”

    And here is code snippet for your reference.

                                if (Session["Access"] == null)

                      {

                            Session.Add("Access", true);

                }

    Hope this could help you.

     

    Best Regards,

    Porter Wang

    • Marked as answer by Ton-Ton Keaton Wednesday, December 8, 2010 5:52 AM
    Wednesday, December 8, 2010 3:13 AM
  • Hi JayGeeBee,

     

                    Please forgive me, shyjumohan. I think you misunderstand me.

    According to MSDN the reason/solution is:
    “When using cookie-based session state, ASP.NET does not allocate storage for session data until the Session object is used. As a result, a new session ID is generated for each page request until the session object is accessed. If your application requires a static session ID for the entire session, you can either implement the Session_Start method in the application’s Global.asax file and store data in the Session object to fix the session ID, or you can use code in another part of your application to explicitly store data in the Session object.”
    bags

    And here is code snippet for your reference.

                                if (Session["Access" ] == null )

                      {

                             Session.Add("Access" , true );

                }

    Hope this could help you.

     

    Best Regards,

    Porter Wang


    This is what I want.
    i want bags
    • Marked as answer by Ton-Ton Keaton Wednesday, December 8, 2010 5:51 AM
    • Unmarked as answer by Ton-Ton Keaton Wednesday, December 8, 2010 5:52 AM
    Wednesday, December 8, 2010 4:49 AM
  • Hi JayGeeBee,

     

                    Please try the following code snippet.

                    OnLoad() {

    Username();

    }

     

    Username() {

    if (Session["Access" ] == null )

                      {

                             Session.Add("Access" , true );

                }

    //put username to database
    }

                In addition, please don’t forget  to enable session in sharepoint site from web.config which was mentioned by  Vijander.

     

     

    Best Regards,

    Porter Wang

    Wednesday, December 8, 2010 5:36 AM
  • Wow. It works. :) the value of session["value"] is null during the first visit, and after  refreshing the page, it still has its value so the code will not write the username to the database anymore, until the user logs out.

    thanks alot.

     


    ----------------------- Sharepoint Newbie
    Wednesday, December 8, 2010 5:51 AM