//Set People picker field to current user by Andy Bonner
_spBodyOnLoadFunctionNames.push("fillDefaultValues");
function fillDefaultValues()
{
fillPeoplePickerWithCurrentUser('Submitted_x0020_By');
}
function fillPeoplePickerWithCurrentUser(pickerName)
{
//get the current user from the welcome menu
var currentUser = getCurrentUser();
//check to see that we've got it
if(currentUser != null)
{
//get the people pickers input div
var pp = getPickerInputElement(pickerName);
//set it to the current user if we've found it
if(pp != null)
pp.innerHTML = currentUser;
}
}
function getCurrentUser()
{
var tags = document.getElementsByTagName('a');
for (var i=0; i < tags.length; i++)
{
if(tags[i].innerText.substr(0,7) == 'Welcome')
{
return tags[i].innerText.substr(8,tags[i].innerText.length);
}
}
}
function getPickerInputElement(fieldsInternalName)
{
var result = "";
var divs = document.getElementsByTagName("DIV");
for(var i=0; i < divs.length ; i++)
{
if(divs[i].id=="WebPartWPQ2")
{
var tds = divs[i].getElementsByTagName("TD");
for(var j=0; j < tds.length; j++)
{
var cellHTML = tds[j].innerHTML;
if(cellHTML.indexOf('FieldInternalName="' + fieldsInternalName + '"') >= 0)
{
var innerDivs = tds[j].getElementsByTagName("DIV");
for(var k=0; k < innerDivs .length; k++)
{
if(innerDivs[k].id.indexOf("UserField_upLevelDiv") > 0)
{
result = innerDivs[k];
break;
}
}
}
}
}
}
return result;
}
Tuesday, April 28, 2009
JavaScript funtion to hide fields on Newform.aspx/Editform.aspx
•TAGNAME: HTML element that is being rendered ("SELECT", "INPUT"...)
•IDENTIFIER: SharePoint field type identifier ("TextField", "DropDownChoice"...)
•FIELD NAME: Display name of the field (e.g. "Status", "Customer Name"...)
Script starts...
_spBodyOnLoadFunctionNames.push("hideFields");
function hideFields() {
var control = getTagFromIdentifierAndTitle("TAGNAME","IDENTIFIER","FIELD NAME");
control.parentNode.parentNode.parentNode.style.display="none";
}
function 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];
}
}
return null;
}
Script ends...
you can also refer to this link
•IDENTIFIER: SharePoint field type identifier ("TextField", "DropDownChoice"...)
•FIELD NAME: Display name of the field (e.g. "Status", "Customer Name"...)
Script starts...
_spBodyOnLoadFunctionNames.push("hideFields");
function hideFields() {
var control = getTagFromIdentifierAndTitle("TAGNAME","IDENTIFIER","FIELD NAME");
control.parentNode.parentNode.parentNode.style.display="none";
}
function 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];
}
}
return null;
}
Script ends...
you can also refer to this link
Labels:
JavaScript
Monday, April 27, 2009
JavaScripts to Disable controls on Newform.aspx/Editform.aspx
JavaScripts to Disable controls on Editform.aspx
for(var i = 0; i < document.aspnetForm.elements.length; i++)
if ((document.aspnetForm.elements[i].title == "Recommendation")) // give the title of the control that you want to disable
{
document.aspnetForm.elements[i].disabled =
true;
}
for(var i = 0; i < document.aspnetForm.elements.length; i++)
if ((document.aspnetForm.elements[i].title == "Recommendation")) // give the title of the control that you want to disable
{
document.aspnetForm.elements[i].disabled =
true;
}
Labels:
JavaScript
JavaScript function to get element using tag name and title
function GetElementByText(tagName, title)
{
var a = document.getElementsByTagName(tagName);
for (var i=0; i < a.length; i++)
{
if (a[i].value)
{
if (a[i].value == title )
{
return a[i];
}
}
}
return null;
}
{
var a = document.getElementsByTagName(tagName);
for (var i=0; i < a.length; i++)
{
if (a[i].value)
{
if (a[i].value == title )
{
return a[i];
}
}
}
return null;
}
Labels:
JavaScript
Add Custom Menu to List Item and Document Library Item
var siteUrl = GetSiteUrl();
function Custom_AddListMenuItems(m, ctx)
{
var copyCurrentItemId = currentItemID;
var titleText = null;
// Add Open Workspace menu item
var strDisplayText = "Request Home";
var strAction = "document.location.href='"+siteUrl+"/DCP/RequestPage.aspx?Request="+titleText+"&ID="+copyCurrentItemId+"'";
CAMOpt(m, strDisplayText, strAction);
CAMSep(m);
// false means that the standard menu items should also rendered
return false;
}
function me()
{
window.location = siteUrl + "/DCP/NewRequestByMgr.aspx?&Source="+GetSource();
}
function GetSiteUrl()
{
var siteUrl = window.location.href;
var copyOfSiteUrl = siteUrl;
siteUrl = siteUrl.split("/");
var isSubSite = false;
var url = null;
if(siteUrl[3] == 'sites')
{
isSubSite = true;
}
if(isSubSite)
{
copyOfSiteUrl = copyOfSiteUrl.split("sites/");
var subSiteName = copyOfSiteUrl[1].split("/");
url = copyOfSiteUrl[0] + "sites/" + subSiteName[0];
return url;
}
else
{
copyOfSiteUrl = copyOfSiteUrl.split("/");
url = copyOfSiteUrl[0] + "//" + copyOfSiteUrl[2];
return url;
}
}
For document library use
function AddDocLibMenuItems(m, ctx)
{
if (typeof(Custom_AddDocLibMenuItems) != “undefined”)
{
if (Custom_AddDocLibMenuItems(m, ctx))
return;
}
}
function instead of Custom_AddListMenuItems(m, ctx)
{
.....
}
function Custom_AddListMenuItems(m, ctx)
{
var copyCurrentItemId = currentItemID;
var titleText = null;
// Add Open Workspace menu item
var strDisplayText = "Request Home";
var strAction = "document.location.href='"+siteUrl+"/DCP/RequestPage.aspx?Request="+titleText+"&ID="+copyCurrentItemId+"'";
CAMOpt(m, strDisplayText, strAction);
CAMSep(m);
// false means that the standard menu items should also rendered
return false;
}
function me()
{
window.location = siteUrl + "/DCP/NewRequestByMgr.aspx?&Source="+GetSource();
}
function GetSiteUrl()
{
var siteUrl = window.location.href;
var copyOfSiteUrl = siteUrl;
siteUrl = siteUrl.split("/");
var isSubSite = false;
var url = null;
if(siteUrl[3] == 'sites')
{
isSubSite = true;
}
if(isSubSite)
{
copyOfSiteUrl = copyOfSiteUrl.split("sites/");
var subSiteName = copyOfSiteUrl[1].split("/");
url = copyOfSiteUrl[0] + "sites/" + subSiteName[0];
return url;
}
else
{
copyOfSiteUrl = copyOfSiteUrl.split("/");
url = copyOfSiteUrl[0] + "//" + copyOfSiteUrl[2];
return url;
}
}
For document library use
function AddDocLibMenuItems(m, ctx)
{
if (typeof(Custom_AddDocLibMenuItems) != “undefined”)
{
if (Custom_AddDocLibMenuItems(m, ctx))
return;
}
}
function instead of Custom_AddListMenuItems(m, ctx)
{
.....
}
Labels:
JavaScript,
SP List
Subscribe to:
Posts (Atom)