SharePoint ECMA Client Object Model JSOM Get ContentType

Code to get ContentType with ECMA Client Object model.

<script type=”text/javascript”>

//Get Page Item from Pages Library with ContentType Name

function GetPageItemContentType() {

var clientContext = new SP.ClientContext.get_current();

var oList = clientContext.get_web().get_lists().getByTitle(“Pages”);

var camlQuery = new SP.CamlQuery();

//we can use this variable to pass dynamic page value
var pagename = “sample.aspx”;

camlQuery.set_viewXml(‘<View><Query><Where><Contains><FieldRef Name=\’FileLeafRef\’ /><Value Type=\’File\’>’ + pagename + ‘</Value></Contains></Where></Query></View>’)

this.collListItem = oList.getItems(camlQuery);

// Include Content Type wich Locads Conent Type
clientContext.load(collListItem, ‘Include(ContentType)’);

clientContext.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFailure));
}

//On success Meethod
function onSuccess() {
var listEnumerator = collListItem.getEnumerator();
while (listEnumerator.moveNext()) {
var currentItem = listEnumerator.get_current();
var contentType = currentItem.get_contentType();
var contentTypeName = contentType.get_name();
alert(“Contenttype Name :” + contentTypeName);
}
}

//On Fail
function onFailure() {
alert(‘Failure!’);
}

//Call ECMA Script Method
ExecuteOrDelayUntilScriptLoaded(GetPageItemContentType, “sp.js”);

</script>

Advertisement

SharePoint CAML Query OrderBy Operation

Get TOP 10 Records depending on metadata column with CAML Query

using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb curWeb = site.OpenWeb())
{
SPList oList = curWeb.Lists.TryGetList(“Hitcount”);

SPQuery query = new SPQuery();

query.Query = string.Concat(
“<OrderBy>”,
“<FieldRef Name=’Counter’ Ascending=’False’ />”,
“</OrderBy>”);

query.RowLimit = 10;

query.ViewFields = string.Concat(
“<FieldRef Name=’Title’ />”,
“<FieldRef Name=’Counter’ />”,
“<FieldRef Name=’URL’ />”);

query.ViewFieldsOnly = true; // Fetch only the data that we need.

SPListItemCollection oListItem = oList.GetItems(query);

DataTable dt = new DataTable();
dt = oListItem.GetDataTable();

GridView1.DataSource = dt;
GridView1.DataBind();

}
}

SharePoint Basic CRUD Operation Insert , Delete , Update , Select All Records

SharePoint Basic CRUD Operation Sample Code

AddItem :

private void AddItem()
{
try
{
using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
SPList oList = oWeb.Lists.TryGetList(“Info”);

SPListItem oListItem = oList.AddItem();

oListItem[“Title”] = “Second”;
oListItem[“Name”] = “Microsoft”;
oListItem[“Contact_x0020_Number”] = “555”;

oListItem.Update();

lblMessage.Text = “Item Added”;
}

}

}
catch (Exception ex)
{
lblMessage.Text = ex.ToString();
}
}

Update Item :

private void UpdateItem()
{
using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
SPList oList = oWeb.Lists.TryGetList(“Info”);

SPListItem oListitem = oList.GetItemById(int.Parse(TextBox1.Text));

oListitem[“Name”] = “Sample Code”;

oListitem.Update();

}
}
}
Delete Item :

private void DeleteItem()
{
using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
SPList oList = oWeb.Lists.TryGetList(“Info”);

SPListItem oListitem = oList.GetItemById(int.Parse(TextBox1.Text));

oListitem.Delete();

lblMessage.Text = “Item Deleted”;
}
}
}

Get All list Item

private void LoadGridview()
{
lblMessage.Text = “Get List Data”;

try
{
using (SPSite oSite = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
SPList oList = oWeb.Lists.TryGetList(“Info”);

SPListItemCollection oListItemColl = oList.Items;

GridView1.DataSource = oListItemColl.GetDataTable();
GridView1.DataBind();

}

}

}
catch (Exception ex)
{
lblMessage.Text = ex.ToString();
}
}

SharePoint JavaScript Client Object Model

 
How to fetch list data from JavaScript in SharePoint

Sample Code

<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js”></script&gt;

<script type=”text/javascript”>

$(document).ready(function () {

SP.SOD.executeOrDelayUntilScriptLoaded(fnClientObject,’SP.js’);

});
</script>

<script type=”text/javascript”>

ExecuteOrDelayUntilScriptLoaded(fnClientObject, “sp.js”);

function fnClientObject() {

var clientContext = new SP.ClientContext.get_current();

var oList = clientContext.get_web().get_lists().getByTitle(‘Pages’);

var camlQuery = new SP.CamlQuery();

//we can use this variable to pass dynamic page value
var pagename = ‘default.aspx’;

camlQuery.set_viewXml(‘<View><Query><Where><Contains><FieldRef Name=\’FileLeafRef\’ /><Value Type=\’File\’>’+ pagename +'</Value></Contains></Where></Query></View>’)

this.collListItem = oList.getItems(camlQuery);

clientContext.load(collListItem);

clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args) {

var listItemInfo = ”;

var itemcode = ”;

var listItemEnumerator = collListItem.getEnumerator();

while (listItemEnumerator.moveNext()) {

var oListItem = listItemEnumerator.get_current();

itemcode = oListItem.get_item(‘ItemCode’)

//alert(“Item Code” + itemcode);
}

}

function onQueryFailed(sender, args) {

alert(‘Request failed. ‘ + args.get_message() + ‘\n’ + args.get_stackTrace());
}

</script>

 

SharePoint CAML Query with FileLeafRef

When we try to use LinkFilename in CAML query which is the internal name for column “Name” we are getting an error and CAML query fail.

But we can use LinkFilename column in a OrderBy clause

if you can’t use LinkFilename in a Where clause, you can alternatively use the FileLeafRef column which contains the document name part of the document URL prefixed with it’s list ID

i.e. ows_ID

sample Code to use FileLeafRef in CAMl Query

string currentPageName = “default.aspx”;

string strCat = string.Empty;

try
{
using(SPSite oSite = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb oweb = oSite.OpenWeb())
{
SPList oLlist = oweb.Lists.TryGetList(“Pages”);

SPQuery query = new SPQuery();
query.Query = “<Where><Contains><FieldRef Name=’FileLeafRef’ /><Value Type=’Text’>” + currentPageName + “</Value></Contains></Where>”;

SPListItemCollection oLlistItem = oLlist.GetItems(query);

foreach (SPListItem item in oLlistItem)
{
strCat = Convert.ToString(item[“ColumnName”]);
}

lblMessage.Text = strCat;
}

}

}
catch (Exception ex)
{
throw ex;
}