SharePoint survey object model

Source: Internet
Author: User

Spfieldratingscale

======================================

Spweb web = spcontrol. getcontextweb (httpcontext. Current );

Guid surveyid = web. Lists. Add ("Name of the title", "Description of the survey", splisttemplatetype. Survey );

Splist survey = web. Lists ["survey list name or ID];

String question = "Question #1 for survey ";

 

Stringcollection choices = new stringcollection ();

Choices. Add ("First Choice ");

Choices. Add ("second choice ");

Choices. Add ("third choice ");

Survey. Fields. Add (question, spfieldtype. Choice, true, false, choices );

==========================================

Below code snippet renders SharePoint survey results as pie chart using telerik chart control:

 

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Web;
Using system. drawing;
Using system. runtime. interopservices;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. xml. serialization;
Using system. Web. UI. htmlcontrols;
Using Microsoft. SharePoint;
Using Microsoft. Sharepoint. webcontrols;
Using Microsoft. Sharepoint. webpartpages;
Using telerik. webcontrols;
Using telerik. charting;
Using telerik. webcontrols. dialogs;

Namespace commoncontrols. Survey
{
[GUID ("ce0403df-d85c-4ba0-9b57-f944fb70ded4")]
Public class surveychart: Microsoft. Sharepoint. webpartpages. webpart
{
List <response> response;
String surveyquestion;
Radchart chart;

Public surveychart ()
{
This. exportmode = webpartexportmode. All;
}

Protected override void rendercontents (htmltextwriter)
{
Htmltextwriter. Write ("<Table> <tr> <TD> ");
This. Chart. rendercontrol (htmltextwriter );
Htmltextwriter. Write ("</table> </TD> </tr> ");
}

Protected override void createchildcontrols ()
{
String [] colors = {"# ffda43", "#2f89c2", "# f267cd", "#79e364", "# eb5d2d", "#42ae18 ", "# ff9c40", "# b99a72", "# 9bc0ff", "#98cc88", "# c3b600 "};
Chart = new radchart ();
Chart. usesession = false;
Chart. tempimagesfolder = "~ /_ Layouts/Temp ";
Chart. radcontrolsdir = "~ /_ Layouts/radcontrols /";

Chartseries = new chartseries ();
Chart. addchartseries (chartseries );
Charttitle Title = new charttitle ();

Chartseries series = chart. getchartseries (0 );
Series. Name = surveyquestion;

Chart. Legend. halignment = charthalignment. Center;
Chart. Legend. valignment = chartvalignment. bottom;
Chart. Legend. legendstyle = legenditemspositiontype. row;

Response = getsurveyresponse ("http: // server/sites/techresources/", "http: // server/sites/techresources/lists/survey2 ");
Title. Text = surveyquestion;
Title. textfont = new font ("verdana", 7 );
Chart. Chart. charttitle = title;

Series. Clear ();
Series. diameterscale = 0.40;
Series. showlabelconnectors = false;
Series. showlabels = true;
Series. labelappearance. Distance = 7;
Series. labelappearance. textfont = new font ("verdana", 7 );
Series. notoverlaplabels = true;
Series. type = chartseriestype. Pie;
Colorconverter cc = new colorconverter ();
Int I = 0;
Foreach (Response res in response)
{
Chartseriesitem seriesitem = new chartseriesitem ();
Seriesitem. yvalue = res. totalresponse;
Seriesitem. itemmap. tooltip = res. choicename;
Seriesitem. Label = res. totalresponse. tostring ();
Seriesitem. Appearance. maincolor = (color) CC. convertfromstring (colors [I ++]);
Seriesitem. Appearance. bordercolor = color. dimgray;
Seriesitem. Appearance. fillstyle = fillstyle. Solid;
Seriesitem. Name = res. choicename;
Series. Items. Add (seriesitem );
}
Chart. Skin = "lightgreen ";
This. Controls. Add (Chart );
}

Public list <response> getsurveyresponse (string siteurl, string surveyname)
{

List <response> responselist;
String questionname = "";
Using (spsite ospsite = new spsite (siteurl ))
{
Using (spweb ospweb = ospsite. openweb ())
{
Splist surveylist = ospweb. getlist (surveyname );
If (surveylist. basetype! = Spbasetype. Survey)
Throw new exception ("list is not of survey type ");
Responselist = new list <response> ();
Foreach (spfield field in surveylist. fields)
{
If (field. type = spfieldtype. choice)
{
Spfieldchoice choicefield;
Choicefield = (spfieldchoice) field;
Questionname = field. staticname;
Surveyquestion = field. title;
Foreach (string choicename in choicefield. Choices)
{
Response resfield = new response ();
Resfield. choicename = choicename;
Responselist. Add (resfield );
}
}
}
Foreach (Response res in responselist)
{
Spquery responsequery = new spquery ();
Responsequery. query = @ "<where>
<EQ> <fieldref name = "+ questionname +"/> <value type = \ "choice \"> "+ res. choicename + @" </value> </EQ>
</Where> ";
Res. totalresponse = surveylist. getitems (responsequery). count;
}
}
}
Return responselist;
}
}
}

========================================================== ===

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.