var productcollection; function getProducts(title) { try { var context = new SP.ClientContext.get_current(); var web = context.get_web(); var list = web.get_lists().getByTitle('product'); var query = '<View Scope=\'RecursiveAll\'>'+ '<Query>'+ '<Where>'+ '<Contains>'+ '<FieldRef Name=\'ProductName\'/>' + '<Value Type=\'Text\'>' + title +'</Value>'+ '</Contains>'+ '</Where>'+ '</Query>'+ '</View>'; var camlQuery = new SP.CamlQuery(); camlQuery.set_viewXml(query); this.productcollection = list.getItems(camlQuery); context.load(this.productcollection, 'Include(ProductName, ProductDescription, ProductType, LaunchDate, AvailableQuantity)'); context.executeQueryAsync(Function.createDelegate(this, this.productsReceived), Function.createDelegate(this, this.failed)); } catch (e) { alert(e); } } function productsReceived() { alert('got products'); prcessProducts(this.productcollection); } function failed(sender, args) { alert('failed. Message:' + args.get_message()); }
Search This Blog
Sunday, September 13, 2015
Search Items from a List
Get and Set The List Item Value in SharePoint 2010 Using ECMAScript
Introduction
In this article you will see how to get and set the list item value in SharePoint 2010 using ECMAScript. I have a custom list named "Custom" which has the following items.
I have two columns "SampleOne" and "SampleTwo". I need to get the "SampleOne" field value and set the same to the "SampleTwo" field using ECMAScript.
Steps Involved
In this article you will see how to get and set the list item value in SharePoint 2010 using ECMAScript. I have a custom list named "Custom" which has the following items.
I have two columns "SampleOne" and "SampleTwo". I need to get the "SampleOne" field value and set the same to the "SampleTwo" field using ECMAScript.
Steps Involved
- Navigate to the SharePoint site (I have a site page created in my site where I am going to add the content editor web part).
- Go to Site Actions, click on Edit Page.
- Click on the Insert tab in the ribbon interface and then click on Web Part button.
- Select Media and Content in the Categories section and then click on Content Editor.
- Click on Add.
- The Content Editor web part will be added to the site page.
- Click on down arrow and then click on Edit Web Part.
- Click on "Click here to add new content".
- Click on the Format Text tab in the ribbon interface and then click on HTML drop down.
- Click on Edit HTML source.
- The HTML source window will pop up.
- Copy and paste the following script.
<script language="ecmascript" type="text/ecmascript">
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("Custom");
this.listItem = list.getItemById(3);
clientContext.load(this.listItem);
this.clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess),
Function.createDelegate(this, this.OnLoadFailed));
}
}
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());
}</script> <input id="btnGetSetListItem" onclick="getSetListItem()" type="button" value="Get & Set List Item" />
- Click on Ok.
- In the ribbon interface click on Save & Close button.
- In the Content Editor web part you can find a button named "Get & Set List Item", click on that button.
- An alert will pop up.
Reference
SP.ListItem.item Property - http://msdn.microsoft.com/en-us/library/ee549053.aspx
SP.ListItem.item Property - http://msdn.microsoft.com/en-us/library/ee549053.aspx
Get the Lookup Field Value For a List Item in SharePoint 2010 Using ECMAScript
Introduction
In this article you will see how to get the lookup field value for a list item in SharePoint 2010 using ECMAScript. I have a custom list named "Custom" which has a lookup column named "LookupSingleValue".
And I have the following item in the list as shown in the following figure.
Steps Involved:
- Navigate to the SharePoint site (I have a site page created in my site where I am going to add the content editor web part).
- Go to Site Actions, click on Edit Page.
3. Click on Insert tab in the ribbon interface and then click on Web Part button.
4. Select Media and Content in the Categories section and then click on Content Editor.
5. Click on Add.
6. The Content editor web part will be added to the site page.
7. Click on the down arrow and then click on Edit Web Part.
6. The Content editor web part will be added to the site page.
7. Click on the down arrow and then click on Edit Web Part.
8. Click on "Click here to add new content".
9. Click on the Format Text tab in the ribbon interface and then click on the HTML drop down.
10. Click on Edit HTML source.
11. the HTML source window will pop up.
11. the HTML source window will pop up.
12. Copy and paste the following script.
<script language="ecmascript" type="text/ecmascript">
var listItem;
var list;
var clientContext;
function getLookUp() {
this.clientContext = SP.ClientContext.get_current();
if (this.clientContext != undefined && clientContext != null) {
var webSite = clientContext.get_web();
this.list = webSite.get_lists().getByTitle("Custom");
this.listItem = list.getItemById(5);
clientContext.load(this.listItem);
this.clientContext.executeQueryAsync(Function.createDelegate(this,this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));
}
}
function OnLoadSuccess(sender, args) {
var lookup = this.listItem.get_item("LookupSingleValue");
alert("Lookup Id: " + lookup.get_lookupId() + "\n Lookup Value: " + lookup.get_lookupValue());
}
function OnLoadFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>
<input id="btnGetLookUp" onclick="getLookUp()" type="button" value="Get Look Up" />
13. Click on Ok.
14. In the ribbon interface click on Save & Close button.
14. In the ribbon interface click on Save & Close button.
15. In the content editor web part you can find a button named "Get Look Up"; click on that button.
16. An alert will pop up.
References
SP.FieldLookupValue.lookupId Property - http://msdn.microsoft.com/en-us/library/ee553739.aspx
SP.FieldLookupValue.lookupValue Property - http://msdn.microsoft.com/en-us/library/ee551154.aspx
Set the Default Value for the Field in SharePoint 2010 using ECMAScript
Introduction
In this article you will see how to set the default value for the field in SharePoint 2010 using ECMAScript. I have a list named "List Request" which has the following fields (Navigate to the list, click on List tab in the ribbon interface. Click on List Settings button).
I am going to set the default value for the column named "Description" using ECMAScript.
Steps Involved
Step 1 : Navigate to the SharePoint site (I have a site page created in my site where I am going to add the content editor web part).
Step 2 : Go to Site Actions, click on Edit Page.
Step 3 : Click on the Insert tab in the ribbon interface and then click on Web Part button.
Step 4 : Select Media and Content in the Categories section and then click on Content Editor.
Step 5 : Click on Add.
Step 6 : The Content Editor web part will be added to the site page.
Step 7 : Click on the down arrow and then click on Edit Web Part.
Step 8 : Click on "Click here to add new content".
Step 9 : Click on Format Text tab in the ribbon interface and then click on HTML drop down.
Step 10 : Click on Edit HTML source.
Step 11 : The HTML source window will pop up.
Step 12 : Copy and paste the following script.
<script language="ecmascript" type="text/ecmascript">
var fieldCollection;
var field;
var list;
function fieldDefaultValue() {
var clientContext = SP.ClientContext.get_current();
if (clientContext != undefined && clientContext != null) {
var webSite = clientContext.get_web();
In this article you will see how to set the default value for the field in SharePoint 2010 using ECMAScript. I have a list named "List Request" which has the following fields (Navigate to the list, click on List tab in the ribbon interface. Click on List Settings button).
I am going to set the default value for the column named "Description" using ECMAScript.
Steps Involved
Step 1 : Navigate to the SharePoint site (I have a site page created in my site where I am going to add the content editor web part).
Step 2 : Go to Site Actions, click on Edit Page.
Step 3 : Click on the Insert tab in the ribbon interface and then click on Web Part button.
Step 4 : Select Media and Content in the Categories section and then click on Content Editor.
Step 5 : Click on Add.
Step 6 : The Content Editor web part will be added to the site page.
Step 7 : Click on the down arrow and then click on Edit Web Part.
Step 8 : Click on "Click here to add new content".
Step 9 : Click on Format Text tab in the ribbon interface and then click on HTML drop down.
Step 10 : Click on Edit HTML source.
Step 11 : The HTML source window will pop up.
Step 12 : Copy and paste the following script.
<script language="ecmascript" type="text/ecmascript">
var fieldCollection;
var field;
var list;
function fieldDefaultValue() {
var clientContext = SP.ClientContext.get_current();
if (clientContext != undefined && clientContext != null) {
var webSite = clientContext.get_web();
this.list = webSite.get_lists().getByTitle("List Request");
this.fieldCollection = list.get_fields();
this.field = fieldCollection.getByTitle("Description");
this.field.set_defaultValue("Default"); this.field.update(); clientContext.load(this.field);
clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess),
Function.createDelegate(this, this.OnLoadFailed));
}
}
function OnLoadSuccess(sender, args) {
alert(this.field.get_defaultValue()); }
function OnLoadFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script> <input id="btnFieldDefaultValue" onclick="fieldDefaultValue()" type="button" value="Field Default Value" />
Step 13 : Click on Ok.
Step 14 : In the ribbon interface click on the Save & Close button.
Step 15 : In the content editor web part you can find a button named "Field Default Value"; click on that button.
Step 16 : An alert will pop up which displays the default value for the field.
Reference
this.fieldCollection = list.get_fields();
this.field = fieldCollection.getByTitle("Description");
this.field.set_defaultValue("Default"); this.field.update(); clientContext.load(this.field);
clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess),
Function.createDelegate(this, this.OnLoadFailed));
}
}
function OnLoadSuccess(sender, args) {
alert(this.field.get_defaultValue()); }
function OnLoadFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script> <input id="btnFieldDefaultValue" onclick="fieldDefaultValue()" type="button" value="Field Default Value" />
Step 13 : Click on Ok.
Step 14 : In the ribbon interface click on the Save & Close button.
Step 15 : In the content editor web part you can find a button named "Field Default Value"; click on that button.
Step 16 : An alert will pop up which displays the default value for the field.
Reference
- SP.Field.defaultValue() Property - http://msdn.microsoft.com/en-us/library/ee547636.aspx
- SP.Field.update() Method - http://msdn.microsoft.com/en-us/library/ee659278.aspx
Get The List Content Types in SharePoint 2010 Using ECMAScript
Introduction
In this article you will see how to get the list content types in SharePoint 2010 using ECMAScript. I have a list named "List Request" which has the following content types (Navigate to the list, click on List tab in the ribbon interface. Click on List Settings button).
In this article you will see how to get the list content types in SharePoint 2010 using ECMAScript. I have a list named "List Request" which has the following content types (Navigate to the list, click on List tab in the ribbon interface. Click on List Settings button).
- Item.
- Folder.
- Central Content Type.
Steps Involved
- Navigate to the SharePoint site (I have a site page created in my site where I am going to add the content editor web part).
- Go to Site Actions, click on the Edit Page.
- Click on the Insert tab in the ribbon interface and then click on the Web Part button.
- Select Media and Content in the Categories section and then click on Content Editor.
- Click on Add.
- The Content Editor web part will be added to the site page.
- Click on the down arrow and then click on Edit Web Part.
- Click on "Click here to add new content".
- Click on Format Text tab in the ribbon interface and then click on HTML drop down.
- Click on Edit HTML source.
- HTML source window will pop up.
- Copy and paste the following script.
<script language="ecmascript" type="text/ecmascript"> var contentTypeCollection;
var listCollection;
var list;
function getListContentTypes() {
var clientContext = new SP.ClientContext.get_current();
if (clientContext != undefined && clientContext != null) {
var web = clientContext.get_web();
this.listCollection = web.get_lists();
this.list = listCollection.getByTitle("List Request");
this.contentTypeCollection = list.get_contentTypes(); clientContext.load(this.contentTypeCollection);
clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
}function onQuerySucceeded() {
var contentType = 'List Content Types:\n ' var contentTypeEnumerator = this.contentTypeCollection.getEnumerator();while (contentTypeEnumerator.moveNext()) {
var content = contentTypeEnumerator.get_current();
contentType += content.get_name() + '\n';
}
alert(contentType);
}function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
- Click on Ok.
- In the ribbon interface click on Save & Close button.
- In the content editor web part you can find a button named "Get List Content Types"; click on that button.
- An alert will pop up displaying the list content types.
Reference
SP.List.contentTypes Property - http://msdn.microsoft.com/en-us/library/ee556719.aspx
SP.List.contentTypes Property - http://msdn.microsoft.com/en-us/library/ee556719.aspx
Get "CreatedBy" and "ModifiedBy" Values From SharePoint 2010 List Using ECMAScript
In this article you will see how to get the "CreatedBy" and "ModifiedBy" values from SharePoint 2010 list using ECMAScript.
Introduction
In this article you will see how to get the "CreatedBy" and "ModifiedBy" values from SharePoint 2010 list using ECMAScript. I have a custom list named "CustomList" which has the following columns:
Introduction
In this article you will see how to get the "CreatedBy" and "ModifiedBy" values from SharePoint 2010 list using ECMAScript. I have a custom list named "CustomList" which has the following columns:
Steps Involved
1. Navigate to the SharePoint site (I have a site page created in my site where I am going to add the content editor web part).
2. Go to Site Actions, click on Edit Page.
2. Go to Site Actions, click on Edit Page.
3. Click on the Insert tab in the ribbon interface and then click on Web Part Button.
4. Select Media and Content in the Categories section and then click on Content Editor.
5. Click on Add.
6. The Content Editor web part will be added to the site page.
7. Click on the down arrow and then click on Edit Web Part.
6. The Content Editor web part will be added to the site page.
7. Click on the down arrow and then click on Edit Web Part.
8. Click on "Click here to add new content".
9. Click on the Format Text tab in the ribbon interface and then click on HTML DropDown.
10. Click on Edit HTML source.
11. The HTML source window will pop up.
11. The HTML source window will pop up.
12. Copy and paste the following script.
<script language="ecmascript" type="text/ecmascript">
var listItem;
var list;
var clientContext;
function getFieldUserValue() {
this.clientContext = SP.ClientContext.get_current();
if (this.clientContext != undefined && clientContext != null) {
var webSite = clientContext.get_web();
this.list = webSite.get_lists().getByTitle("CustomList");
this.listItem = list.getItemById(1);
clientContext.load(this.listItem);
this.clientContext.executeQueryAsync(Function.createDelegate(this,this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));
}
}
function OnLoadSuccess(sender, args) {
var fieldUserValueCreatedBy = this.listItem.get_item("Author");
var fieldUserValueModifiedBy = this.listItem.get_item("Editor");
alert("Created By: " + fieldUserValueModifiedBy.get_lookupValue() + "\n Modified By: "+ fieldUserValueModifiedBy.get_lookupValue() + "\n");
}
function OnLoadFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>
<input id="btnGetFieldUserValue" onclick="getFieldUserValue()" type="button" value="Get Created by and Modified by" />
13. Click on Ok.
14. In the ribbon interface click on the Save & Close button.
14. In the ribbon interface click on the Save & Close button.
15. In the content editor web part you can find a button named "Get Created By and Modified By"; click on that Button.
16. An alert will pop up.
Reference
- SP.FieldUserValue.lookupValue Property - http://msdn.microsoft.com/en-us/library/ee550746.aspx
Subscribe to:
Posts (Atom)