none
Share:How to programmatically check whether a user has responded to a Survey or not. RRS feed

  • General discussion

  •  

    Usually we use the follow SPQuery to make sure whether a user  has given a answer to our surveys:

    Here we suppose you run these code in the administrator's role



    string siteUrl="http://****";
    string userForCheck="Domain\\useraccount";
    //Check whether the user has responded to the survey
    bool result=false;
    //Check whether the user has completed the answer
    bool isRespondComplete=false;
     SPSecurity.RunWithElevatedPrivileges(
    delegate()
        {

         
    using(SPSite site =new SPSite(siteUrl))
         {

              
    using (SPWeb web=site.RootWeb)
              { 
                 SPUser imitationUser 
    = web.EnsureUser(userForCheck);
                 
    using(SPSite siteWithUser = new SPSite(siteUrl, user.UserToken))
                 {
                     
    using ( SPWeb webWithUser = siteWithUser.RootWeb)
                     {
                        SPQuery q 
    = new SPQuery();
                        q.Query
    =@"<Where>
                                      <Eq>
                                   <FieldRef Name=\
    "Author\"/>
                                   <Value Type=\"User\">"+SPUser.Name+@"</Value>"
                                      </Eq>
                                  
    </Where>"
                        SPList list=webWithUser.Lists["your survey's name"];
                        SPListItemCollection items
    =list.GetItems(query);       
      
                        
    if(items.Count>0)
                        {
                              result
    =true;
                              isRespondComplete
    =items[0]["Completed"].ToString()=="1";
                        }
                     }
                 }  
              }
         }
         });


    WPF
    Thursday, October 16, 2008 1:28 PM

All replies


  • Ok .. first off.

    You don't need all those "Using" statements. You only need a "Using" on the SPSite. When that statement finished it will automatically dispose of all the objects created in it.

    Secondly, don't use site.Rootweb .. it is a property value, so if you disose it you can break things. I recommend using site.OpenWeb(""); instead.

    Thirdly .. you don't need to impersonate the user or re-create all of the SPSite and SPWeb objects to execute your CAML query.
    Simply searching the list using the Author=SPUser .Name should do the trick.
    regards
    Martin Hatch
    MCPD .Net Web Development
    MCTS WSS 3.0 | MOSS 2007
    Visit my Blog
    Thursday, October 16, 2008 2:54 PM
  • PS - thanks for sharing though :)
    regards
    Martin Hatch
    MCPD .Net Web Development
    MCTS WSS 3.0 | MOSS 2007
    Visit my Blog
    Thursday, October 16, 2008 2:54 PM
  • Martin, and everyone..a using statement, when compiled, basically wraps a try, catch, finally around the code being run and performs a call to dispose in the finally statement, therefore guaranteeing that the resource will be disposed of even if the code throws an exception. It does not do a recursive check of open objects created inside it and dispose of them too - therefore multiple using statements nested together is OK.

    You can, however, "use" multiple objects in the using signature and can also create the objects outside the using signature e.g:

    SPSite oSite = new SPSite("");
    using(oSite)
    {

    }

    or

    SPSite oSite = new SPSite("");
    SPSite oSite2 = new SPSite("");
    using(oSite, oSite2)
    {

    }
    http://www.starznet.co.uk/sharepoint/blog Please mark replies as answered if they have helped answer your question
    • Edited by Stuart Starrs Thursday, October 16, 2008 3:18 PM Lack of explanation
    Thursday, October 16, 2008 3:14 PM
  • But don't any resource being created within a try/catch block (or any construct for that matter ... if statements or loops) get disposed when the construct terminates?

    Also, in reference to SharePoint, any objects created from the used object (e.g. site.OpenWeb("")) would be disposed, as they are related to the original object.
    regards
    Martin Hatch
    MCPD .Net Web Development
    MCTS WSS 3.0 | MOSS 2007
    Visit my Blog
    Thursday, October 16, 2008 4:30 PM
  • Hi,first thanks for attention.
    About the "using" statements,i just  follow the Microsoft's "Best Practices".You can find it here:http://msdn.microsoft.com/en-us/library/aa973248.aspx

    There is a reason for me to impersonate the user to execute the CAML query ,and that's the point of this thread.
    Assume you have a survey with branches,and some users may click the "Save" button when they answer to it  which will create a item with the field "Completed" with "No".
    Then you will get a number of "All Responses"  with 20,but actually there are 19 items in your "Show All Responses" List.
    The lost item can only be seen by the user who created it for its field "Completed" is "No".

    So when we check whether a user has responded to a survey or not ,we need to confirm that the user is "has not responded" or " has responded but has not completed"
    WPF
    Friday, October 17, 2008 1:44 AM
  • Martin Hatch said:

    But don't any resource being created within a try/catch block (or any construct for that matter ... if statements or loops) get disposed when the construct terminates?

    Also, in reference to SharePoint, any objects created from the used object (e.g. site.OpenWeb("")) would be disposed, as they are related to the original object

    Ibelieve Anything constructed within the try will be placed on the "to free" list.
    However things used in a using construct () explicitly have the Dispose() method called in the finally block - other items do not, even if they implement IDisposable.

    so
    using (myClass foo)
    {
    myClass bar;
    .....
    }

    dispose will be called on foo but *not* bar;

    Objects you construct directly and call Dispose() on are assumed to do the "right thing" with objects they constructed that implement IDisposable.

    Friday, October 17, 2008 5:15 AM