First Set the j query refference into your page.
<script type="text/javascript" src="../../Scripts/jquery-1.7.2.min.js"></script>
Get the query string value in the edit or display page.
<Script type="text/javascript">
function GetQueryStringParams(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}</Script>
Finaly write the ecmca sript (Which will load on page
load)
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(getSetListItem, "sp.js");
var itemId=GetQueryStringParams('id');
var listItem;
var list;
var clientContext;
function getSetListItem() {
this.clientContext = SP.ClientContext.get_current();
if (this.clientContext != undefined && clientContext != null) {
var webSite = clientContext.get_web();
this.list = webSite.get_lists().getByTitle("ENS Email");
this.listItem = list.getItemById(itemId);
clientContext.load(this.listItem);
this.clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess),
Function.createDelegate(this, this.OnLoadFailed));
}
}
function OnLoadSuccess(sender, args) {
var approval=false;
approval= this.listItem.get_item("Approval_x0020_Needed");
//where Approval_x0020_Needed is a yes/no field in the list
if (approval==true)
{
//to show or hide the list items
$("nobr:contains('Approval Status')").
parent('h3').parent('td').parent('tr').show();
$("nobr:contains('Approval Comments')").
parent('h3').parent('td').parent('tr').show();
}
else
{
//to show or hide the list items
$("nobr:contains('Approval Status')").
parent('h3').parent('td').parent('tr').hide();
$("nobr:contains('Approval Comments')").
parent('h3').parent('td').parent('tr').hide();
}
}
function OnLoadFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
To set the value use following
function OnLoadSuccess(sender, args) {
var value = this.listItem.get_item("SampleOne");
this.listItem.set_item("SampleTwo", value);
this.listItem.update();
this.clientContext.load(this.listItem);
this.clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess1),
Function.createDelegate(this, this.OnLoadFailed));
}
function OnLoadSuccess1(sender, args) {
alert(this.listItem.get_item("SampleTwo"));
}
function OnLoadFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
you can also call the function like
<input id="btnGetSetListItem" onclick="getSetListItem()" type="button" value="Get & Set List Item" />
<script type="text/javascript" src="../../Scripts/jquery-1.7.2.min.js"></script>
Get the query string value in the edit or display page.
<Script type="text/javascript">
function GetQueryStringParams(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}</Script>
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(getSetListItem, "sp.js");
var itemId=GetQueryStringParams('id');
var listItem;
var list;
var clientContext;
function getSetListItem() {
this.clientContext = SP.ClientContext.get_current();
if (this.clientContext != undefined && clientContext != null) {
var webSite = clientContext.get_web();
this.list = webSite.get_lists().getByTitle("ENS Email");
this.listItem = list.getItemById(itemId);
clientContext.load(this.listItem);
this.clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess),
Function.createDelegate(this, this.OnLoadFailed));
}
}
function OnLoadSuccess(sender, args) {
var approval=false;
approval= this.listItem.get_item("Approval_x0020_Needed");
//where Approval_x0020_Needed is a yes/no field in the list
if (approval==true)
{
//to show or hide the list items
$("nobr:contains('Approval Status')").
parent('h3').parent('td').parent('tr').show();
$("nobr:contains('Approval Comments')").
parent('h3').parent('td').parent('tr').show();
}
else
{
//to show or hide the list items
$("nobr:contains('Approval Status')").
parent('h3').parent('td').parent('tr').hide();
$("nobr:contains('Approval Comments')").
parent('h3').parent('td').parent('tr').hide();
}
}
function OnLoadFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
To set the value use following
function OnLoadSuccess(sender, args) {
var value = this.listItem.get_item("SampleOne");
this.listItem.set_item("SampleTwo", value);
this.listItem.update();
this.clientContext.load(this.listItem);
this.clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess1),
Function.createDelegate(this, this.OnLoadFailed));
}
function OnLoadSuccess1(sender, args) {
alert(this.listItem.get_item("SampleTwo"));
}
function OnLoadFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
you can also call the function like
<input id="btnGetSetListItem" onclick="getSetListItem()" type="button" value="Get & Set List Item" />
No comments:
Post a Comment