0

One of the Projects I am working on, I have a requirement to read the SharePoint site description on page load and then based on the read description, i have to hide/show web parts.

So my site description for a site looks like: "Finance;Team site for Finance department."

Now using JSOM, I want to check if the site description contains the keyword Finance or not. Based on the value returned true or false, I have to do something. Below is the code I wrote which alerts me Site Description on page load but I am stuck at how to see if a site description contains the word Finance or not. Can someone please help me resolve this. Many thanks. Right now I am using .includes method to check if the description contains certain keyword or not. FYI, I am working with SharePoint online.

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">   

$(document).ready(function() { 
        ExecuteOrDelayUntilScriptLoaded(Main, "sp.js");

    });
    function Main()
    { 
        GetSiteInfo();
    }

function GetSiteInfo()
    {
    var clientContext = new SP.ClientContext.get_current();
    this.oWebsite = clientContext.get_web();
    clientContext.load(this.oWebsite);       

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

    function onQuerySucceeded(sender, args) 
    {   
        var Sitedes= this.oWebsite.get_description();        
        UpdateTiles(Sitedes);   
    alert(Sitedes);          
     }    

function failed(sender, args)
{
  alert("failed. Message:" + args.get_message());
} 

function UpdateTiles(des)
{
    var stagevalue = des.includes("Finance");

    switch(stagevalue){
    case 'true':
    $('#MSOZoneCell_WebPartWPQ5').hide();
    $('#MSOZoneCell_WebPartWPQ6').show();
    alert("Web part 1 hidden");
    break;

    case 'false':   
    $('#MSOZoneCell_WebPartWPQ5').show(); 
    $('#MSOZoneCell_WebPartWPQ6').hide();
    alert("Web part 2 hidden");
    break;

    case 'default':
    alert("something went wrong");
    break;
}   
}   
</script>
0

.includes returns a Boolean. A true or false, not a string like 'true' or 'false'. Also, .includes is not supported in all browsers (like IE 11).

Consider changing the .includes line from:

var stagevalue = des.includes("Finance");

to:

var stagevalue = des.indexOf("Finance") >= 0;

For the test, try something like this:

if(stagevalue) {
  $('#MSOZoneCell_WebPartWPQ5').hide();
  $('#MSOZoneCell_WebPartWPQ6').show();
}
else
{
  $('#MSOZoneCell_WebPartWPQ5').show(); 
  $('#MSOZoneCell_WebPartWPQ6').hide();
}
  • Smith, exactly that is what I changed with reference to one of the blogs and it worked like a charm. Thanks so much for your response. – mdevm Apr 17 '18 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.