3

Is there a way to place a small JQuery/Javascript code to detect if an item list value is equal to yes and if so redirect the page using Content Editor Webpart?

If "Status=no"
 Then <meta http-equiv="refresh" content="0; url=http://example.com/" /> 

The hard part is reading the item list value and integrating that into JavaScript.

1 Answer 1

2

If I summarize your requirement, Then It is

  1. Get Items from a list using JavaScript
  2. Redirect to some URL

Get Items Using JSOM

You can use CAML query for this as you need to filter Status=no. So the query should look like

<View>
   <Query>
      <Where>
        <Eq><FieldRef Name=\"Status\" />
           <Value Type=\"Text\">no</Value>
        </Eq>
      </Where>
    </Query>
</View>

Sample code

function retrieveListItems() {

    var clientContext = new SP.ClientContext('Site Url');
    var oList = clientContext.get_web().get_lists().getByTitle('List Name');

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\"Status\" /><Value Type=\"Text\">no</Value></Eq></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 listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo += '\nID: ' + oListItem.get_id() +
            '\nTitle: ' + oListItem.get_item('Title') +
            '\nStatus: ' + oListItem.get_item('Status');
    }

    alert(listItemInfo.toString());
}

function onQueryFailed(sender, args) {

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

For more explanation How to: Retrieve List Items Using JavaScript

Redirect to some URL

window.location.href = 'http://example.com/';
3
  • Thank you Atish! Please forgive by letting me, since this is for one page, where do I place the code above? In a CEWP? Thank you! Commented Jan 6, 2016 at 15:45
  • This is a little new to me. Please tell me , is the aforementioned code to be placed in a CEWP. Furthermore I can't ascertain where the retrieved fields are stored. Commented Jan 8, 2016 at 14:54
  • From which page you need to redirect. You can put this code into that page Commented Jan 8, 2016 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.