您的位置:首页 > 其它

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";

}

}

}

}

}

});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐