jQuery

ECMA Script SP.ClientContext to Json

Posted on Updated on

Want to convert to Json a list.getItems collection? this is the way using ECMA Javascript

Declare SP.js

<SharePoint:ScriptLink runat="server" Name="SP.js" Localizable="false" OnDemand="False" LoadAfterUI="True"></SharePoint:ScriptLink> 

 

JavaScript Function

function getJson() {
    var context = new SP.ClientContext("/");
    var list = context.get_web().get_lists().getByTitle("YourList"); 
    var viewXml = "<View><RowLimit>1000</RowLimit></View>";
    var query = new SP.CamlQuery();
    query.set_viewXml(viewXml);
    var items = list.getItems(query);
    context.load(items, "Include(Column1,Column2,Column3)");

    function onLoaded() {
        var myItems = [];
        var itemsCount = items.get_count();
        for (var i = 0; i < itemsCount; i++) {
            var item = items.itemAt(i);
            var myItem = JSON.stringify(item.get_fieldValues());
            myItems.push(myItem);
        }
        alert(myItems);
    }

    context.add_requestSucceeded(onLoaded);

    function onFailure() {

    }

    context.add_requestFailed(onFailure);
    context.executeQueryAsync();
} 
 
UPDATE
 
In order to use JSON.parse() you must change the [Object<Array>] like this
 
var dt = "[" + MyItems + "]";
var ret = JSON.parse(MyItems); 

It will give you a [Object],[Object],[Object]

Advertisements

Change SharePoint Master Page PlaceHolderPageTitle with jQuery

Posted on Updated on

In SharePoint, the object “PlaceHolderPageTitle” is responsible to render the Title. So if you have a property to bind the text, you should be able to render like this:

Property to include object titles

<SharePoint:ProjectProperty Property="Title" runat="server" />

In the Master Page

<SharePoint:PageTitle runat="server">
      <asp:ContentPlaceHolder id="PlaceHolderPageTitle" runat="server"><SharePoint:ProjectProperty Property="Title" runat="server" /></asp:ContentPlaceHolder>
 </SharePoint:PageTitle>  
 
The Span that Renders the Place Holder is:
 
<span id="DeltaPlaceHolderPageTitleInTitleArea">
     <asp:ContentPlaceHolder ID="PlaceHolderPageTitleInTitleArea" runat="server" />
</span>  
 

Using jQuery, you can change what ever you want, in this case the object Title.

I suggest to use ExecuteOrDelayUntilScriptLoaded because in this case you are sure that the DOM is loaded, preventing conflicts between jQuery functions

Change Title

<script type="text/javascript">
  ExecuteOrDelayUntilScriptLoaded(function () {    
    $('span#DeltaPlaceHolderPageTitleInTitleArea').each(function () {      
         $(this).empty();
         $(this).append(“OK i want this one”);
    });
}, "sp.js"); 
</script> 
 
Change if specific number of characters reached
 
<script type="text/javascript">
  ExecuteOrDelayUntilScriptLoaded(function () {    
    $('span#DeltaPlaceHolderPageTitleInTitleArea').each(function () {
        if ($(this).text().length >= 103) {
            var tituloPageTitle = $(this).text().substr(0, 100) + "...";
            $(this).empty();
            $(this).append(tituloPageTitle);
        }
    });
}, "sp.js");
</script> 
 

Good Luck 😉