1,804 articles and 14,753 comments as of Sunday, April 3rd, 2011

EndUserSharePoint has combined resources with NothingButSharePoint.com. You can now find End User (Mark Miller), Developer (Jeremy Thake) and IT Pro SharePoint (Joel Oleson) content all in one place!

This site is a historical archive and is no longer being updated. Please update your favorites, bookmarks and RSS feeds.

NothingButSharePoint.com
Friday, April 16, 2010

Edit SharePoint Field properties, including lookup column list association, with client side code only

Guest Author: Alexander Bautz
SharePoint JavaScripts

Alexander has updated the code to include “a small fix regarding showing the “Title” field as an available field, changed to DisplayName for the Field label and to add the “Title” column as an editabel column”.

This code enables you to edit the following field properties using jQuery only:

  • Hidden: Specifies whether the field is displayed in the list.
  • ReadOnly: Specifies whether values in the field can be modified.
  • Required: Determines whether the field requires values.
  • Show In NewForm: Specifies whether the field is displayed in the form that is used to create list items.
  • Show In DispForm: Specifies whether the field is displayed in the form for displaying list items.
  • Show In EditForm: Specifies whether the field is displayed in the form that is used to edit list items.
  • Show In List Settings: Specifies whether the field is displayed in the page for customizing list settings.
  • Show In Version History: Specifies whether the field is displayed in the page for viewing list item versions.
  • Show In View Forms: Specifies whether the field is displayed in pages that are used to view list data.
  • Lookup column list association: Change the list association for a lookup column – Note: If you do this, all existing elements in this column will be lost.



Disclaimer:
I have removed some list types and some column types, but be careful using this tool. Test this in a “test list” before you use it in your “production environment”. If you edit the wrong field, you could break the list. Use this tool at your own risk!

Create a WebPartPage, insert a CEWP, and paste this code (change the reference to jQuery):

};
wrapSoapRequest(wsBaseUrl + ‘lists.asmx’, ‘http://schemas.microsoft.com/sharepoint/soap/GetListCollection’, xmlStr, function(data){
if ($(’ErrorText’, data).length > 0) {
result.success = false;
} else {
result.success = true;
var arrTypesToSkip = ['110','111','112','113','114','115','116','117','118'];
$(’List’, data).each(function(){
if($.inArray($(this).attr(’ServerTemplate’),arrTypesToSkip)==-1){
result.lists.push($(this));
}
});
}
});
return result;
}

function getFieldProperties(){
$("#insertListfieldControlsHere").html(”);
listGuid = $("#insertListCollectionHere").find(’option:selected’).val();

var list = customGetList(listGuid);
listName = list.name;
var textStyleHeader = "style=’border-bottom:1px silver solid;border-right:1px silver solid;text-align:center;padding:0 10 0 3′";
var textStyleBody = "style=’border-bottom:1px silver solid;border-right:1px silver solid;text-align:left’";
var tHeader = ”;
tHeader += "<tr valign=’top’ align=’left’ style=’background-color:#CCCCCC’>";
tHeader += "<th style=’border-bottom:1px silver solid;padding:0 10 0 3′>Name</th>";
tHeader += "<th title=’Specifies whether the field is displayed in the list.’ "+textStyleHeader+">Hidden</th>";
tHeader += "<th title=’Specifies whether values in the field can be modified.’ "+textStyleHeader+">ReadOnly</th>";
tHeader += "<th title=’Determines whether the field requires values.’ "+textStyleHeader+">Required</th>";
tHeader += "<th title=’Specifies whether the field is displayed in the form that is used to create list items.’ "+textStyleHeader+">Show In NewForm</th>";
tHeader += "<th title=’Specifies whether the field is displayed in the form for displaying list items.’ "+textStyleHeader+">Show In DispForm</th>";
tHeader += "<th title=’Specifies whether the field is displayed in the form that is used to edit list items.’ "+textStyleHeader+">Show In EditForm</th>";
tHeader += "<th title=’Specifies whether the field is displayed in the page for customizing list settings.’ "+textStyleHeader+">Show In List Settings</th>";
tHeader += "<th title=’Specifies whether the field is displayed in the page for viewing list item versions.’ "+textStyleHeader+">Show In Version History</th>";
tHeader += "<th title=’Specifies whether the field is displayed in pages that are used to view list data.’ "+textStyleHeader+">Show In View Forms</th>";
tHeader += "<th title=’Change the list association for a lookup column’ "+textStyleHeader+">Lookup column list association</th>";
tHeader += "<th title=” "+textStyleHeader+">Save changes</th>";
tHeader += "</tr>";

$("#insertListfieldControlsHere").append(tHeader);
var tbody = ”;
fieldsObj = {};
$.each(list.fields,function(i,field){
var name = field.attr(’Name’);
fieldsObj[name]=field;
var dispName = field.attr(’DisplayName’);
var fieldType = field.attr(’Type’);
var tdStr = "<td style=’border-bottom:1px silver solid;border-right:1px silver solid;text-align:left;vertical-align:top;padding:2 5 0 3′>"+dispName+"</td>";
// Hidden
tdStr += "<td "+textStyleBody+">"+
"<input type=’radio’ name=’"+name+"_hidden’ propName=’Hidden’ value=’TRUE’ /><label>Yes</label><br />"+
"<input type=’radio’ name=’"+name+"_hidden’ propName=’Hidden’ value=’FALSE’ /><label>No</label><br />"+
"</td>";
// ReadOnly
tdStr += "<td "+textStyleBody+">"+
"<input type=’radio’ name=’"+name+"_readOnly’ propName=’ReadOnly’ value=’TRUE’ /><label>Yes</label><br />"+
"<input type=’radio’ name=’"+name+"_readOnly’ propName=’ReadOnly’ value=’FALSE’ /><label>No</label><br />"+
"</td>";
// Required
tdStr += "<td "+textStyleBody+">"+
"<input type=’radio’ name=’"+name+"_required’ propName=’Required’ value=’TRUE’ /><label>Yes</label><br />"+
"<input type=’radio’ name=’"+name+"_required’ propName=’Required’ value=’FALSE’ /><label>No</label><br />"+
"</td>";
// ShowInNewForm
tdStr += "<td "+textStyleBody+">"+
"<input type=’radio’ name=’"+name+"_showInNewForm’ propName=’ShowInNewForm’ value=’TRUE’ /><label>Yes</label><br />"+
"<input type=’radio’ name=’"+name+"_showInNewForm’ propName=’ShowInNewForm’ value=’FALSE’ /><label>No</label><br />"+
"</td>";
// ShowInDispForm
tdStr += "<td "+textStyleBody+">"+
"<input type=’radio’ name=’"+name+"_showInDisplayForm’ propName=’ShowInDisplayForm’ value=’TRUE’ /><label>Yes</label><br />"+
"<input type=’radio’ name=’"+name+"_showInDisplayForm’ propName=’ShowInDisplayForm’ value=’FALSE’ /><label>No</label><br />"+
"</td>";
// ShowInEditForm
tdStr += "<td "+textStyleBody+">"+
"<input type=’radio’ name=’"+name+"_showInEditForm’ propName=’ShowInEditForm’ value=’TRUE’ /><label>Yes</label><br />"+
"<input type=’radio’ name=’"+name+"_showInEditForm’ propName=’ShowInEditForm’ value=’FALSE’ /><label>No</label><br />"+
"</td>";
// ShowInListSettings
tdStr += "<td "+textStyleBody+">"+
"<input type=’radio’ name=’"+name+"_showInListSettings’ propName=’ShowInListSettings’ value=’TRUE’ /><label>Yes</label><br />"+
"<input type=’radio’ name=’"+name+"_showInListSettings’ propName=’ShowInListSettings’ value=’FALSE’ /><label>No</label><br />"+
"</td>";
// ShowInVersionHistory
tdStr += "<td "+textStyleBody+">"+
"<input type=’radio’ name=’"+name+"_showInVersionHistory’ propName=’ShowInVersionHistory’ value=’TRUE’ /><label>Yes</label><br />"+
"<input type=’radio’ name=’"+name+"_showInVersionHistory’ propName=’ShowInVersionHistory’ value=’FALSE’ /><label>No</label><br />"+
"</td>";
// ShowInViewForms
tdStr += "<td "+textStyleBody+">"+
"<input type=’radio’ name=’"+name+"_showInViewForms’ propName=’ShowInViewForms’ value=’TRUE’ /><label>Yes</label><br />"+
"<input type=’radio’ name=’"+name+"_showInViewForms’ propName=’ShowInViewForms’ value=’FALSE’ /><label>No</label><br />"+
"</td>";
// Lookup columns change list assosiation
if($(this).attr(’Type’)==’Lookup’ || $(this).attr(’Type’)==’LookupMulti’ && $(this).attr(’List’)!=undefined){
tdStr += "<td "+textStyleBody+">"+listSelectLookupReassosiation+"</td>";
}else{
tdStr += "<td "+textStyleBody+">&nbsp;</td>";
}

// Save changes button
tdStr+="<td style=’border-bottom:1px silver solid;text-align:center’><input onclick=’javascript:submitChangedFieldProperties(\"tr_"+name+"\")’ type=’button’ value=’save’></td>";

// Append to table
$("#insertListfieldControlsHere").append("<tr id=’tr_"+name+"’ name=’"+name+"’ dispName=’"+dispName+"’ fieldType=’"+fieldType+"’>"+tdStr+"</tr>");

// Lookup column list re-association
if($(this).attr(’Type’)==’Lookup’ || $(this).attr(’Type’)==’LookupMulti’ && $(this).attr(’List’)!=undefined){
var currentListAssociation = $(this).attr(’List’).toLowerCase();
$("#insertListfieldControlsHere tr[id='tr_"+name+"'] select[propName='List'] option").each(function(i,opt){
if(opt.value.toLowerCase()==currentListAssociation){
opt.selected=true;
}
});
}

// Check the correct radio button for existing properties
// Hidden
var hidden = $(this).attr(’Hidden’);
if(hidden!=’undefined’){
$("#insertListfieldControlsHere input[name='"+name+"_hidden'][value='"+hidden+"']").attr(’checked’,true);
}
// ReadOnly
var readOnly = $(this).attr(’ReadOnly’);
if(readOnly!=’undefined’){
$("#insertListfieldControlsHere input[name='"+name+"_readOnly'][value='"+readOnly+"']").attr(’checked’,true);
}
// Required
var required = $(this).attr(’Required’);
if(required!=’undefined’){
$("#insertListfieldControlsHere input[name='"+name+"_required'][value='"+required+"']").attr(’checked’,true);
}
// ShowInNewForm
var newForm = $(this).attr(’ShowInNewForm’);
if(newForm!=’undefined’){
$("#insertListfieldControlsHere input[name='"+name+"_showInNewForm'][value='"+newForm+"']").attr(’checked’,true);
}
// ShowInDispForm
var dispForm = $(this).attr(’ShowInDisplayForm’);
if(dispForm!=’undefined’){
$("#insertListfieldControlsHere input[name='"+name+"_showInDisplayForm'][value='"+dispForm+"']").attr(’checked’,true);
}
// ShowInEditForm
var editForm = $(this).attr(’ShowInEditForm’);
if(editForm!=’undefined’){
$("#insertListfieldControlsHere input[name='"+name+"_showInEditForm'][value='"+editForm+"']").attr(’checked’,true);
}
// ShowInListSettings
var listSettings = $(this).attr(’ShowInListSettings’);
if(listSettings!=’undefined’){
$("#insertListfieldControlsHere input[name='"+name+"_showInListSettings'][value='"+listSettings+"']").attr(’checked’,true);
}
// ShowInVersionHistory
var versionHistory = $(this).attr(’ShowInVersionHistory’);
if(versionHistory!=’undefined’){
$("#insertListfieldControlsHere input[name='"+name+"_showInVersionHistory'][value='"+versionHistory+"']").attr(’checked’,true);
}
// ShowInViewForms
var viewForms = $(this).attr(’ShowInViewForms’);
if(viewForms!=’undefined’){
$("#insertListfieldControlsHere input[name='"+name+"_showInViewForms'][value='"+viewForms+"']").attr(’checked’,true);
}
});
}

function customGetList(listName){
xmlStr = [];
xmlStr.push(’<GetList xmlns="http://schemas.microsoft.com/sharepoint/soap/">’);
xmlStr.push(’<listName>’ + listName + ‘</listName>’);
xmlStr.push(’</GetList>’);
xmlStr = xmlStr.join(”);
var result = {success:false,fields:[]};
wrapSoapRequest(wsBaseUrl + ‘lists.asmx’, ‘http://schemas.microsoft.com/sharepoint/soap/GetList’, xmlStr, function(data){
if($(’ErrorText’, data).length > 0) {
result.success = false;
}else{
result.success = true;
result.name = $(’List’, data).attr(’Name’);
var arrOfTypesToSkip = ['Computed','Calculated'];
$(’Field’, data).each(function(){
// Title field
if($(this).attr(’StaticName’)==’Title’){
result.fields.push($(this));
}else if($(this).attr(’FromBaseType’)!=’TRUE’ && $(this).attr(’Sealed’)!=’TRUE’ && $(this).attr(’DisplayName’)!=undefined && $.inArray($(this).attr(’Type’),arrOfTypesToSkip)==-1){
result.fields.push($(this));
}
});
}
});
return result;
}

function submitChangedFieldProperties(trId){
var trObj = $("#"+trId);
var name = trObj.attr(’name’);
var currFieldObj = fieldsObj[name];
var changed = false;
trObj.find(’input:radio:checked’).each(function(){
var propName = $(this).attr(’propName’);
var checked = $(this).attr(’checked’);
var val = $(this).val();
if(currFieldObj.attr(propName)!=val){
currFieldObj.attr(propName,val);
changed = true;
}
});
// Lookup column list re-association
trObj.find(’select option:selected’).each(function(){
var propName = $(this).parent().attr(’propName’);
var val = $(this).val();
if(currFieldObj.attr(propName)!=val && val!=”){
currFieldObj.attr(propName,val);
changed = true;
}
});

// Convert xml object to string
if(window.ActiveXObject){
var stringFromXML = currFieldObj[0].xml;
}else{
var stringFromXML = (new XMLSerializer()).serializeToString(currFieldObj[0]);
}

if(changed){
var saveOK = onclickUpdateFieldProperties(listName,stringFromXML);
if(saveOK.success){
trObj.find(’input:button’).hide().parent().html("<span>Saved OK</span>").parent().css({’background-color’:'#A6D785′});
trObj.find(’input:button’).attr(’disabled’,true);
}else if(!saveOK.success){
trObj.find(’input:button’).hide().parent().html("<span>Error occured</span>").parent().css({’background-color’:'#F08080′});
trObj.find(’input:button’).attr(’disabled’,true);
}
}else{
alert("You haven’t made any changes!");
}
}

function onclickUpdateFieldProperties(listName,fieldXML){
xmlStr = [];
xmlStr.push(’<UpdateList xmlns="http://schemas.microsoft.com/sharepoint/soap/">’);
xmlStr.push(’<listName>’ + listName + ‘</listName>’);
xmlStr.push(’<listProperties><List/></listProperties>’);
xmlStr.push(’<newFields><Fields></Fields></newFields>’);
xmlStr.push(’<updateFields>’);
xmlStr.push(’<Fields>’);
xmlStr.push(’<Method ID="1">’);
xmlStr.push(fieldXML);
xmlStr.push(’</Method>’);
xmlStr.push(’</Fields>’);
xmlStr.push(’</updateFields>’);
xmlStr.push(’<deleteFields><Fields></Fields></deleteFields>’);
xmlStr.push(’</UpdateList>’);
xmlStr = xmlStr.join(”);

var result = {success:false};
wrapSoapRequest(wsBaseUrl + ‘lists.asmx’, ‘http://schemas.microsoft.com/sharepoint/soap/UpdateList’, xmlStr, function(data){
if ($(’ErrorText’, data).length > 0) {
result.success = false;
} else {
result.success = true;
}
});
return result;
}

function wrapSoapRequest(webserviceUrl,requestHeader,soapBody,successFunc){
var xmlWrap = [];
xmlWrap.push("<?xml version=’1.0′ encoding=’utf-8′?>");
xmlWrap.push("<soap:Envelope xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance’ xmlns:xsd=’http://www.w3.org/2001/XMLSchema’ xmlns:soap=’http://schemas.xmlsoap.org/soap/envelope/’>");
xmlWrap.push("<soap:Body>");
xmlWrap.push(soapBody);
xmlWrap.push("</soap:Body>");
xmlWrap.push("</soap:Envelope>");
xmlWrap = xmlWrap.join(”);
$.ajax({
async:false,
type:"POST",
url:webserviceUrl,
contentType:"text/xml; charset=utf-8",
processData:false,
data:xmlWrap,
dataType:"xml",
beforeSend:function(xhr){
xhr.setRequestHeader(’SOAPAction’,requestHeader);
},
success:successFunc
});
}
</script>

Guest Author: Alexander Bautz
SharePoint JavaScripts

Alexander Bautz is a SharePoint consultant/developer (mainly JavaScript/jQuery solution) living in Norway. Alexander spends a lot of his spare time blogging on the same topics. His focus area is "end user customizations" with no (or as little as possible) server side code.

View all entries in this series: Alexander Bautz»
 

Please Join the Discussion

11 Responses to “Edit SharePoint Field properties, including lookup column list association, with client side code only”
  1. Eric says:

    Alexander;

    Fascinating solution, but on which page(s) do you actually put the CEWP?

    -Eric

    • Eric says:

      Sorry, disregard the stupid question… missed the basic intent of providing a web part page from which any list can be configured. Thanks for a very elegant solution!

      -Eric

  2. Drew says:

    Hi Alexander,

    Thanks for this post. This code will be very useful to me, however since I’m a neophyte when it comes to javascript and Jquery so I want to make sure I understand what i need to do here.

    When you say to update the reference to Jquery, do you mean that I would need to have Jquery installed on my SharePoint Web Front End Server and then change the path from

    to the location of my jquery .js file?

    • Eric says:

      Drew;

      jQuery isn’t “installed”, it’s just a reference file full of script. In Alexander’s code he’s calling the script from a library in his SharePoint site collection. Since his relative URL says “/test/English/Javascript/jquery-1.3.2.min.js” the absolute URL would be something like “http://toplevelsite/test/English/Javascript/jquery-1.3.2.min.js”.

      You can either put jQuery into a document library that’s globally accessible from somewhere near the top level of your site collection (recommended) or call it from an online source like http://code.jquery.com/jquery-1.4.2.min.js. After that you’d modify the reference in the script to your chosen location, as in “http://toplevelsite/jquerylibrary/jquery-1.4.2.min.js” (using the server relative URL of course).

      I’m not expert enough to fully understand HOW Alexander’s script works (that’s the beauty of jQuery for power users like me) but I’ve implemented it and it works like a charm!

      -Eric

  3. Drew says:

    You might also guide me on the issue of where to install the actual .js file on my server, assuming my previous assumption is correct!

    With thanks!

  4. tecrms says:

    Great post!

  5. Bill Garvey says:

    Love it! I can only do Client side work, so I can’t install Office Toolbox from codeplex.

    Is there any combination of boxes that allow editing on new form, but display only on edit and disp forms? That would rock!

    Thanks for all the great posts!

  6. Miguel says:

    Hi Alex, very nice solution is really fantastic!!

    works perfect… but…
    I tried to modify a workflow associated to the list and unfortunately see that all the fields I’ve modified (hide from a form) have disapear!!!

    if I try to add a new condition or action within the workflow I can’t see any of those fields I have modified, even if I revert the visibility to Yes doesn’t work
    is that a known side effect?

    maybe I did something wrong
    please help!!
    thanks
    miguel (from Chile)

  7. Miguel says:

    additional information on my previous message…
    all the workflows are working fine, the problem is that I can’t see the fields no more on the workflow editor on Sharepoint Designer…

    if anyone have experience this problem, help will be appreciated
    thanks
    miguel

  8. Nina says:

    Seems like the upper part is missing. I can’t see anything to change the jquery reference.


Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!