Thursday, February 5, 2015

SharePoint Site title update by using JavaScript

SharePoint Site title update by using JavaScript
<script language="javascript" type="text/javascript">
// <![CDATA[

     //Ensure the Client Object Model is ready before you use it
    ExecuteOrDelayUntilScriptLoaded(GetTitle, "sp.js");

    function Button1_onclick() {
    //Update the site's Title property   
        site.set_title(document.getElementById("Text1").value);
        //  site.update();
        site.update();
        // addd the site to Query queue       
        context.load(site);

        //Run the Query on the server     
        context.executeQueryAsync(onTitleUpdate, onQueryFailed);
    }
 
    function onTitleUpdate() {
        alert('Title has been changed');
             //Refresh the page
               SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
    }

   var site; var context;
   function GetTitle() {
   //Get the current client context
    context = SP.ClientContext.get_current();
    //Add the site to query queue
    site = context.get_web();
    context.load(site);
    //Run the query on the server
    context.executeQueryAsync(onQuerySucceeded, onQueryFailed); }


function onQueryFailed(sender, args) {
        alert('Request failed' + args.get_message() + '\n' + ar.get_stackTrace());
    }

    function onQuerySucceeded(sender, args) {
        document.getElementById("Text1").value = site.get_title();
    }
// ]]>
</script>

<p>
    <input id="Text1" type="text" /></p>
<p>
    <input id="Button1" type="button" value="button" onclick="return Button1_onclick()" /></p>

No comments:

Post a Comment