All posts by Alexander Bautz

SPJS Poll for SharePoint v2.0

I have brushed up the 5 year old solution Poll for SharePoint, to change the deprecated “Google Image Charts to use Google Charts (AKA Google Visualization). The Image Charts are officially deprecated, but will probably continue working for some time, but to ensure you have a working solution, you should upgrade to this new version.

Please note that there are some changes to the Content Editor Web Part code, so existing users must not only update the script file, but also look over the CEWP code and make some small changes.

This code lets you generate polls without the need for server side installed WebParts.
Change log
March 15, 2015
v2.0 released. This one has no new functionality, but the code has been brushed up, and now the charts are generated using “Google Charts” rather than “Image Charts”.
Poll

IMG

Result with column chart

IMG

How to set it up
Create a custom list with the following fields
  • Answer: Single line of text
  • Question: Single line of text

Name it anything you like, but keep the display name fairly simple (no special characters) as you will use the display name in the CEWP code.

CEWP code

The CEWP code below refers jQuery from Google. If you have a local copy of jQuery you can change the script src. You find the code for the file “SPJS-Poll.js” at the bottom of the page.

NOTE: You must change the script src for the file “SPJS-Poll.js” and “spjs-utility.js” to point your instance of the files – the CEWP code will not work unless you do this.
Place this code where you want the poll to appear:
<div id="SPJS_Poll"></div>
<link type="text/css" href="/Scripts/Poll/spjs_poll.css" rel="stylesheet">
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="/Scripts/spjs-utility/spjs-utility.js"></script>
<script type="text/javascript" src="/Scripts/Poll/SPJS-Poll.js"></script>
<script type="text/javascript">

// Preload the chart solution
google.load("visualization", "1", {"packages":["corechart","table"]});

// If you want to override these texts, uncomment the object and localize as you like.
/* 
	spjs.poll.text = {
		"submitLinkText":"Submit",
		"backLinkText":"Back",
		"showResultBtnText":"Show result",
		"pollNotActiveText":"The poll is not active prior to {0}",
		"pollEndedText":"The poll ended on {0}",
		"alreadyRespondedText":"You answered: ",
		"totalResponsesLabel":"Total responses: ",
		"chartLegendLabels":["Answer","Count"]

	};
*/

spjs.poll.init({pollAnswerListName:'Poll',
				listBaseUrl:L_Menu_BaseUrl,				
				id:"Poll_3", // Allowed characters id a-z, 0-9 - and _
				start:"01/02/2015", // format: mm/dd/yyyy
				end:"03/15/2015", // format: mm/dd/yyyy
				singleResponse:false,
				q:"What is your favorite junk food?",
				qStyle:"font-size:1.5em;color:#444;",
				aStyle:"font-size:xx-small",
				a:["Pizza","Hot dog","Hamburger","Neither of them"], // Leave empty for free input				
				color:["red","green","blue","orange"],
				forceLowerCaseAnswer:false, // Group result by lowercase				
				chart:"col", // table, bar, col or pie
				height:200,
				width:500});

</script>

Object attributes explained

  • pollAnswerListName: DisplayName or GUID of the list that stores the answers
  • listBaseUrl: The baseUrl of the site. This is like “/sites/hr” when the list is located in the site “hr” under “/sites”. Use L_Menu_BaseUrl (or omit the property) for current site.
  • id: The unique id of the poll. All poll answers are stored in a list and this id is used to separate each poll
  • start: Start date in the format mm/dd/yyyy
  • end: End date in the format mm/dd/yyyy
  • singleResponse: true for one reply per user, false for unlimited number of replies
  • q: Poll question. To have a linefeed in the question, use <br>
  • qStyle: CSS syntax style
  • aStyle: CSS syntax style
  • a: Answers in an array format. To use free input and not predefined answers, leave the array empty.
  • color: Colors for the chart in an array format. This must have the same length as the previous parameter – one color for each answer
  • forceLowerCaseAnswer: Primarily for use with free input to avoid getting two “series” when the only difference are uppercase characters.
  • chart: “bar” for bar chart, “col” for column chart, “pie” for pie chart or “table” for a plain table.
  • height: Height in pixels
  • width: Width in pixels
Regarding free input

If you leave the attribute “a” as an empty array, the user can supply free text as “answer”. When using free input, the result are automatically presented as a table.

Download code

The code for the file SPJS-Poll.js. You find spjs-utility.js here.

Questions or feedback

Post any questions in the forum

Alexander

DFFS v4.25x still has a few bugs

Hi,
Unfortunately, DFFS v4.25x still has some bugs. There were made a few fundamental changes to how “initial value” was read, and this new query fails if the list contains more that “the allowed” number of lookup columns – this is columns of type “people picker”, “lookup”, “managed metadata / taxonomy fields”. The number of allowed lookup columns is set on the server side, so I will have to rethink how this query is performed.

I’m currently working on a fix, and will post an updated version during this weekend.

Post any questions or feedback here: https://spjsblog.com/forums/forum/dynamic-forms-for-sharepoint/

Sorry for the inconvenience,
Alexander

DFFS v4.250 released

This release has multiple changes and additions from the previous production release (v4.200). You find the complete changelog her: https://spjsblog.com/dffs/dffs-change-log/

I will show you one enhancement in particular – the new options for side-by-side headings and labels:
IMG

This layout is achieved with this configuration
In the “Tabs” tab I have added this configuration:

IMG
Please note how the headers are moved to the side-by-side column using the “Header ID”.

In the Side-by-side tab I have added these labels:

IMG

In the Custom CSS section in the Misc tab I have added this code:
.dffs-vertical-text{
text-align:center;
transform: rotate(-90deg);
font-size:22px;
}
td.sbs_tdIndex_1, td.sbs_tdIndex_2, td.sbs_tdIndex_3, td.sbs_tdIndex_4{
	width:350px !important;
}
td.sbs_Field input.ms-long, td.sbs_Field div.sp-peoplepicker-topLevel{
	width:250px !important;
}
Comments and feedback

Please add any comments, questions or feedback in the forum: https://spjsblog.com/forums/forum/dynamic-forms-for-sharepoint/

Alexander

Cascading dropdowns updated to v3.30

I have posted a small update to SPJS-Casc.js to fix these issues:

  • Added support for autofilling values when using “Autofill subsequent dropdowns when they contain only one valid option.” in DFFS.
  • Changed the jQuery variable from $ to spjs.$ to fix a bug with SharePoints internal file “assetpicker.js” as this one kills jQuery.

Get the updated version here

You find the article describing the solution here, and you find the dedicated forum here.

Alexander

SPJS-Utility.js is updated to v1.200

I have updated SPJS-utility.js to v1.200. I have changed the file so all functions are now part of the “spjs.utility” namespace. I have included support for existing code using the old function names as well. There are not many changes to the functionality, but a few small changes to setFieldValue and getFieldValue related to various bugs reported from DFFS.

Get the updated version here

This file has always contained “internal” functions used in various solutions, but you can use the functions in your custom code. Unfortunately there is not documentation made at this point, but you can post questions in the SPJS-Utility forum.

Alexander

SPJS-Lookup: Convert a single line textfield to a dropdown select based on a query

Change log

You find the latest change log here: https://spjsblog.com/dffs/dffs-change-log/

v1.11 (August 17, 2015):
Fixed an error resulting in existing values not validating when loadign an item in EditForm.

v1.10 (February 28, 2015):
If you want to have the same list of choices in multiple fields, you can now populate an unlimited number of fields from one single query. All you have to do is to use an array of fields in the parameter “fieldToConvertToDropdown”. See code example below for details.

v1.05 (February 12, 2015):
Added option to specify a folder in the query. The custom CAML or query will search only in the specified folder. Please note that you must update spjs-utility.js to v1.205 or later.

v1.04:
Removed a border around an image that occurred in SP 2010.

v1.03:
Fix for “addToExternalList” when using the solution for multiple fields in a form, and more than one targetted the same lookup list. The callback would refresh the bottom SPJS-Lookup field as the “argument object” was not uniquely identified.

January 21, 2015
v1.02:
Fixed a bug where I had mistakenly used the display name and not the FieldInternalName as identifier for the fields.

This is a remake of a solution I posted in 2009. It does mostly the same as the old one, but the code is overhauled, and it is now compatible with DFFS.

What does it do?

This solution is used to convert a single line of text field into a dropdown select. The options for this select is the result of a query you build in the function call. You can use it to query any list within the same site collection. You have an option to add new values to the “lookup list” on the fly, or to enter a value free hand.

This solution is compatible with SP2007, SP 2010 and SP2013.

DFFS plugin

This is compatible with DFFS, but you will not find a dedicated “tab” in DFFS to set it up. You must therefore use the same code for both DFFS and standalone use. With DFFS you have the option to put the function call in the Custom JS section in the Misc tab.

The code

If you use it as a standalone solution, you must refer jQuery and SPJS-utility.js in addition to SPJS-lookup.js.

In a standalone setup it will look something like this
<script type="text/javascript" src="/code/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="/code/spjs-utility.js"></script>
<script type="text/javascript" src="/code/spjs-lookup.js"></script>
<script type="text/javascript">
// Put the contents from the code block below here.
</script>

If you use it with DFFS, the only extra script you need is this (in the DFFS_frontend CEWP) – put it below the reference to spjs-utility.js:

<script type="text/javascript" src="/code/spjs-lookup.js"></script>
The function call
spjs.lookup.init({
	"fieldToConvertToDropdown":["MyTextField"],	
	"listName":"Tasks",
	"listBaseUrl":"/Sites/MySite",
	"optTextFieldInternalName":"Title",
	"sortFieldName":"Title",
	"filterObj":{
		"on":true,
		"folder":"", // Leave empty to search in all folders
		"CAML":null, // If used, the rest of the filterObj settings are disregarded
		"fin":"Completed",
		"isLookup":false,
		"operator":"Neq",
		"filterVal":"1"
	},
	"dropDownDefaultvalue":"...",
	"addYouOwnValue":{
		"on":true,
		"linkText":"Write your own value"
	},
	"addToExternalList":{
		"on":false,
		"customFunction":null, // Function name as a string. If a function name is supplied, this will be used in stead of the default function. The function will be passed the argument object as a parameter.
		"linkText":"Add new item",
		"saveNewItemText":"Save new item"
	},
	"debug":false
});
Parameter details
  • fieldToConvertToDropdown: This is an array or FieldInternalNames of the fields in the current list that you want to convert to a dropdown. Specify multiple fields like this: [“FirstField”,”SecondField”] to have all server the same options based on one single query.
  • listName: This is the display name or the list GUID of the list you read the options from.
  • listBaseUrl: This is the base URL of the list you read the options from. If the list is on the root site of your domain, the value will be an empty string like this “”. If it is on a managed path, it will be something like this: “/Sites/MySite”
  • optTextFieldInternalName: This is the FieldInternalName of the field that represents the options you want to show in the dropdown select.
  • sortFieldName: This is the FieldInternalName of the field you want to sort the options by. Most likely the same as “optTextFieldInternalName”.
  • filterObj
    • on: true or false to tell if the options should be filtered. If false, all options will be shown.
    • folder: Here you can provide a relative URL to a folder like this: /Sites/MySite/Lists/MyList/MyFolder/MySubFolder
    • CAML: Here you can provide the full CAML query to filter by. If this is left as null, the other options below will take effect.
    • fin: The FieldInternal name you want to filter on.
    • isLookup: true or false. If you filter by a text value, use false. If you filter by an ID in a lookup field, set it as true.
    • operator: Use anu valid CAML operator like “Eq”, “Neq”, “BeginsWith” or “Contains”.
    • filterVal: This is the value you want to filter by.
  • dropDownDefaultvalue: This is the default value in the dropdown when it has not been selected.
  • addYouOwnValue
    • on: true or false. This controls whether or not to show a link to “kill” the dropdown and show the underlaying text field.
    • linkText: This is text on the link.
  • addToExternalList
    • on: true or false. This controls whether or not to show a link to add an item to the list you are pulling the options from.
    • customFunction: If you want to override the built “addToExternalList” function, add your custom function name here like this: “myAddListItemFunc”. The function itself must be present in the page, and it will get the full “argObj” passed to it as a parameter.
    • linkText: The link text that initiates the “addToExternalList” function.
    • saveNewItemText: The text on the “save” link.
  • debug: true or false. If true, the underlaying text field will not be hidden, and you will see a yellow information panel in the top of the page.
Setting a value in a field with this solution activated

To set the value of a field when using this solution, use code like this:

spjs.lookup.setValue("FieldInternalName_of_your_field","The value you want to set");

This will also work with DFFS and will trigger any rule currently configured for the underlaying text field (by triggering the blur-event).

Questions or feedback

Please use the forum for all questions related to this solution.

Alexander