Responsive Ads Here

Friday, November 9, 2012

Show all Surveys that user has the permissions

In SharePoint, whenever you want to collect the responses from various people,across your organization about any event, any activity or any other thing,Survey is best suited list. It allows you to collect the responses in various ways. You can ask the questions and they can answer those questions and then result can be analysed by taking it to the excel sheet or through graphical summary or by watching all responses at once.

You can have as many questions as you want and the format of their answers can also be of great varieties. For example, you may want to ask one question whose answer is simple text or multiple line of text with formatting. You can also have a question whose answer is choice (selection in terms of radio buttons,check boxes, drop downs). You can have question whose answer can be a date. We can have all these with built in survey list.

In my recent project I have a requirement to display surveys which user has permission to respond in home page and how many replies for that survey. So in home page user can see the surveys which he can respond and title of the survey and replies of that survey. Once he will respond for that survey it will not be visible.

This is the custom code I have written to achieve the functionality


public void FindSurvey()  
  {  
    string author;  
    DataTable dt = surveyTable();  
    try  
    {  
     System.Security.Principal.WindowsImpersonationContext wic = null;  
     wic = System.Security.Principal.WindowsIdentity.GetCurrent().Impersonate();  
     using (SPSite site = new SPSite(SPContext.Current.Web.Url)) /  
      {  
      using (SPWeb web = site.OpenWeb())  
      {  
        SPListCollection lists = web.Lists;  
        foreach (SPList list in lists)  
         {  
          string str = list.BaseTemplate.ToString();  
          SPContentTypeCollection types = list.ContentTypes;  
          if (str.Contains("Survey"))  
           {  
              if (list.DoesUserHavePermissions(web.CurrentUser, SPBasePermissions.ViewListItems))  
       {  
         DataRow dr = dt.NewRow();  
          dr["Title"] = list.Title.ToString();  
    dr["URL"] = list.Forms[PAGETYPE.PAGE_NEWFORM].ServerRelativeUrl + "?Source=" + web.Url;  
         SPQuery query = new SPQuery();  
         author = SPContext.Current.Web.CurrentUser.Name;  
                   query.Query = String.Format("<Where><Eq><FieldRef Name='Author'/><Value Type='User'>{0}</Value></Eq></Where>", author);  
                   if (list.GetItems(query).Count > 0)  
                   {  
                     dr["Status"] = "Completed";  
                   }  
                   else  
                   {  
                     dr["Status"] = " Not Completed";  
                   }  
                   dr["Date"] = list.Created;  
                   dt.Rows.Add(dr);  
                 }  
               }  
             }  
             DataView dv = new DataView(dt);  
             dv.Sort = "Date desc";  
             gv.DataSource = dv;  
             gv.DataBind();  
           }  
         }  
         wic.Undo();  
       }  
       catch (Exception ex)  
       {  
         
       }  
     }  
     public DataTable surveyTable()  
     {  
       DataTable dt = new DataTable();  
       try  
       {  
         dt.Columns.Add("Title", typeof(string));  
         dt.Columns.Add("URL", typeof(string));  
         dt.Columns.Add("Status", typeof(string));  
         dt.Columns.Add("Date", typeof(DateTime));  
       }  
       catch (Exception ex)  
       {  
         
       }  
       return dt;  
     }  

No comments:

Post a Comment