How to programmatically check whether a user has responded to a Survey or not?
2008-10-16 21:24
591 查看
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:
Code
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";
}
}
}
}
}
});
Here we suppose you run these code in the administrator's role:
Code
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";
}
}
}
}
}
});
相关文章推荐
- How To Check Whether Physical Standby is in Sync with the Primary or Not?
- How to check whether the patches have been applied or not
- facebook permissions : How to check if the user has already allowed publish_stream for your app
- 奇葩问题:This file could not be checked in because the original version of the file on the server was moved or deleted. A new version of this file has been saved to the server, but your check-in comments were not saved
- don't know whether it's worth to check or not brix cms
- How to check whether XPCOM has been registered successfully
- How to Check Whether API Server is up or Down
- how to check whether image object has value
- How to detect whether a module is managed code or not
- Maven-010-maven 编译报错:Failure to ... in ... was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced.
- The concurrent snapshot for publication 'xxx' is not available because it has not been fully generated or the Log Reader Agent is not running to activate it
- A read-only user or a user in a read-only database is not permitted to disable
- How to Check Device UUID or File System UUID. (文档 ID 1505398.1)
- Create a program the checks all the links in your web page whether they are active or not.
- How to write an application that supports the Fast User Switching feature by using Visual Basic .NET or Visual Basic
- How does Gradle know whether to do a single or multiproject build?
- 第15题 To Check the given linked list is palindrome or not
- Node reboot or eviction: How to check if your private interconnect CRS can transmit network heartbea
- MetaException(message:file:/user/hive/warehouse/xxxx is not a directory or unable to create one)
- How to Check Missing Authorisation for User