Monday, May 19, 2014

How to display survey questions directly links in another page.. (New, Resume)

I had a requirement to provide survey links in another page. It is difficult as it is requiring validating whether they already complete the survey or not. I have achieved this by creating an application page.
Here is a code for the application page 

protected void Page_Load(object sender, EventArgs e)
        {

            string siteURL = Request.QueryString["webURL"];  // the url 
            string listName = Request.QueryString["ListName"]; // name simlar to "1";
            string thankYouURL = Request.QueryString["thankYouURL"];

            using (SPSite site = new SPSite(siteURL))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    string userName = web.CurrentUser.Name;

                    SPList list = web.Lists.TryGetList(listName);
                    SPQuery query = new SPQuery();
                    query.Query = string.Format(
                                    @"<Where>
                                        <And>   
                                            <Eq><FieldRef Name='Completed' /><Value Type='Computed'>255</Value></Eq>
                                            <Eq><FieldRef Name='Author' /><Value Type='User'>{0}</Value></Eq>
                                        </And>
                                    </Where>", userName); // CAML Query 
                    SPListItemCollection listItems = list.GetItems(query);
                    if (listItems.Count > 0)
                    {
                        redirectionURL = list.DefaultEditFormUrl + "?ID=" + listItems[0].ID + "&IsDlg=1&Source=" + thankYouURL;
                    }
                    else
                    {
                        redirectionURL = list.DefaultNewFormUrl +  "&IsDlg=1&Source=" + thankYouURL;
                    }

                }

            }
            SPUtility.Redirect(redirectionURL, SPRedirectFlags.Default, System.Web.HttpContext.Current);
            //Response.Redirect(redirectionURL);

        }

Add the application page link on your page with appropriate query String, then job done.

ex:: http://yourSP/mdle1/_layouts/myApplication/SurveyRedirector.aspx?webURL=http://yourSP/sites/sample/myList&ListName=MyList&thankYouURL=welcome.aspx

No comments:

Post a Comment