Friday, December 24, 2010

Editcontrolblock-customaction-for-sharepoint


Editcontrolblock-customaction-for-sharepoint

Thursday, December 23, 2010

Using Javascript code to show/hide controls in NewForm or EditForm.

You can use the following javascript code to show/ hide controls in NewForm or EditForm.

< script type="text/javascript" >

_spBodyOnLoadFunctionNames.push("hideFieldsOnStart");

function hideFieldsOnStart() {

//hide the control at startvar control = getTagFromIdentifierAndTitle("input","TextField","FieldDisplayName");

control.parentNode.parentNode.parentNode.style.display="none";

//add an onchange event to the dropdown
getTagFromIdentifierAndTitle("select","DropDownChoice","DropDownDisplayName").onchange = function() {ChangeEvent()};


}


function ChangeEvent()
{

//get the dropdownvar dropdown = getTagFromIdentifierAndTitle("select","DropDownChoice","DropDownDisplayName");

//get the selected valuevar option = dropDown.options[dropDown.selectedIndex].text;

//get the controlvar control = getTagFromIdentifierAndTitle("input","TextField","FieldDisplayName");

//show hide based on your conditionif(option == "xyz")
{
control.parentNode.parentNode.parentNode.style.display="";
}
else
{
control.parentNode.parentNode.parentNode.style.display="none";
}

//this gets the field based on title identifier and tagnamefunction getTagFromIdentifierAndTitle(tagName, identifier, title) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tags.length; i++) {
var tempString = tags[i].id;
if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
return tags[i];
}
}
returnnull;
}
</script >

List of sharepoint column types and their corresponding identifiers, tagnames and Title


































































SharePoint Field Type Identifier Tag Name Title
Single Line of Text TextField input Same as Field Title
Multiple Lines of Text TextField input Same as Field Title
Number TextField input Same as Field Title
Currency TextField input Same as Field Title
Choice (dropdown) DropDownChoice select Same as Field Title
Lookup (single)* Lookup select Same as Field Title
Lookup (multiple) SelectCandidate; SelectResult select Same as Field Title
Yes/No BooleanField input Same as Field Title
People or Group UserField_downlevelTextBox textarea People Picker

(It must be

People Picker only. Irrespective of Filed name)


It depends what type of field you are trying hide it is in your case
if text field it should be
var control = getTagFromIdentifierAndTitle("input","TextField","FieldDisplayName");

if choice

var control = getTagFromIdentifierAndTitle("select","DropDownChoice","FieldDisplayName");

if yes/no then

var control = getTagFromIdentifierAndTitle("input","BooleanField","FieldDisplayName");

if Lookup then

var control = getTagFromIdentifierAndTitle("select","Lookup","FieldDisplayName");

If it is People Picker


var control = getTagFromIdentifierAndTitle('textarea','UserField_downlevelTextBox','People Picker');

Adding Context Menu in SharePoint List or Library using Content Editor Web part

< script type="text/javascript">
function Custom_AddListMenuItems(m, ctx) {
CAMOpt(m,'Edit Form(new window)','javascript:window.open(\'EditForm.aspx?ID=' + currentItemID + '\');','/_layouts/images/LIST.GIF');
// false means that the standard menu items should also be rendered
// if you set true, it will show only the menu which you added( standard sharepoint menu items will not be available.
// you can control by setting the value of true or false
return true;
}
</script>

If the above function returns false, then the context menu is displayed as below.




If the above function returns true, then the context menu is displayed as below.



Similary if you want to do for List, you have to use the Different method name.

For Document Library : Instead "Custom_AddDocLibMenuItems"

For List : "Custom_AddListMenuItems"

Also you can do following using Script in Content Editor Webpart

To hide the "Delete" (particular) menu item in LibraryContext Menu add this below function

function CAMOpt(p,wzText,wzAct,wzISrc,wzIAlt,wzISeq,wzDesc)
{
var mo=CMOpt(wzText,wzAct,wzISrc,wzIAlt,wzISeq,wzDesc);
if(!mo)return null;
//wzText you can give which one you want hide (like "Edit Properties")
if(wzText != "Delete") AChld(p,mo);
return mo;
}

You can download the script and add it in your content editor webpart; it will take care of the previous actions for document Library.

Use the approriate Script for list / library.
Enjoy!!!...

Using javascript to hide People Picker SharePoint field based on DropDown value

< script type="text/javascript">

_spBodyOnLoadFunctionNames.push("hidePeoplePicker");

function hidePeoplePicker() {
//alert("HIDE");
var control = getTagFromIdentifierAndTitle('textarea','UserField_downlevelTextBox','People Picker');
control.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.style.display="none";

//This code runs on the onchange event of the drop down list field
getField('select','Form Type').onchange = function() {getSecurityLevelValue()};

}

function showPeoplePicker() {
var control = getTagFromIdentifierAndTitle('textarea','UserField_downlevelTextBox','People Picker');
control.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.style.display="";

}




function getTagFromIdentifierAndTitle(tagName, identifier, title) {

//alert("tagName = " + tagName);

var len = identifier.length;
//alert("Len = " + len);

var tags = document.getElementsByTagName(tagName);
//alert("tags length = " + tags.length);

for (var i=0; i < tags.length; i++) {
var tempString = tags[i].id;
var tt = tempString.indexOf(identifier);
var ss = tempString.length - len;
if(tags[i].title == "People Picker")
//alert("tempString = " + tt +"---"+ss );
if (tags[i].title == title && (identifier == "" ||
tempString.indexOf(identifier) == tempString.length - len)) {
return tags[i];
}
}
return null;
}




function getField(fieldType,fieldTitle) {

var docTags = document.getElementsByTagName(fieldType);

//alert("Length = " + docTags.length);

for (var i=0; i < docTags.length; i++) {

if (docTags[i].title == fieldTitle) {

//alert("Value in getField = " + docTags[i].value);

return docTags[i];

}

}

}

function getSecurityLevelValue() {

//alert("In getSecurityLevelValue");
selectedId = getField('select','Form Type').options[getField('select','Form Type').selectedIndex].value;

//alert("Value in hide =" + selectedId);

if(selectedId != 'Org change/ cost centre change')

{

//alert("NOT Specific Request Hide");

hidePeoplePicker();

}

else

{

//alert("Request Show");
showPeoplePicker();

}

}

</script>

Note:-

1) If you have more than one People Picker either in edit form or new form, in that case hidden field should be displayed first and then non hidden people picker in that form. It is done by changing Column order.