0

Edit 12/4: I'm also attempted to get the site page url from another list but also giving me an error each time it tries to open the page from SitePages library. (Posted here)


I'm trying to dynamically create a link to open the appropriate calendar view based on current users

  • List: Staff Directory; Columns: EmailAddress, Section
  • Library: SitePages; Columns: Name, Section

The code below works to get the section name of the current user:

$(function(){
var currentUserEmail = _spPageContextInfo.userEmail;
    var requestSecUrl = "https://site/_api/web/Lists/getByTitle('StaffDirectory')/Items?$filter=Title eq '" + currentUserEmail + "'&$select=Section";
    $.ajax({
      url: requestUrl,
      method: "GET",
      async: false,
      headers: { "Accept": "application/json; odata=verbose" },
      success: function (data) {
      var mySection = data.d.results[0].Section
      alert(mySection);
     },
     error: function (data) {alert ("error found"); }
    });

But how do I use the 'mySection' to filter the library and return the filename?

I tried adding the following after the alert(my Section);

var calSecUrl = "https://site/_api/web/Lists/getByTitle('SitePages')/Items?$filter=Section eq '" + mySection + "'&$select=FileLeafRef";
$.ajax({
      url: calSecUrl,
      method: "GET",
      async: false,
      headers: { "Accept": "application/json; odata=verbose" },
      success: function (data) {
      var CalFileName = data.d.results[0].FileLeafRef;
      alert(CalFileName);
      },
      error: function (data) {alert("Calendar not found");}
      });

I keep getting the "Calendar not found" alert.

  • To get the FileLeafRef try this: http://site/_api/web/Lists/GetByTittle('DocLibName')/items?$select=FieldValuesAsText/FileLeafRef&$expand=FieldValuesAsText – MickB Dec 1 '17 at 21:54
  • It still didn't work. I read somewhere that FileLeafRef is not working for SharePoint Online? "/_api/web/Lists/getByTitle('SitePages')/Items?$select=FileLeafRef&$expand=FieldValuesAsText"; – Tamras Merin Dec 1 '17 at 22:31
1

Use the following code to check if it works.

<script src="//code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
    var currentUserEmail = _spPageContextInfo.userEmail;

    // begin work to call across network
    var requestUri = _spPageContextInfo.webAbsoluteUrl +
                  "/_api/web/Lists/getByTitle('StaffDirectory')/Items?$filter=Title eq '" + currentUserEmail + "'&$select=Section";

    // execute AJAX request
    $.ajax({
        url: requestUri,
        type: "GET",
        headers: { "ACCEPT": "application/json;odata=verbose" },
        success: function (data) {
            if(data.d.results.length>0){
                var mySection = data.d.results[0].Section;
                if(mySection!=null){
                    getFileName(mySection);
                }                               
            }       
        },
        error: function () {
            alert("error found");
        }
    });
});
function getFileName(mySection){
    // begin work to call across network
    var requestUri = _spPageContextInfo.webAbsoluteUrl +
                  "/_api/web/Lists/getByTitle('Site Pages')/Items?$filter=Section eq '" + mySection + "'&$select=FileLeafRef";

    // execute AJAX request
    $.ajax({
        url: requestUri,
        type: "GET",
        headers: { "ACCEPT": "application/json;odata=verbose" },
        success: function (data) {
            if(data.d.results.length>0){
                var CalFileName = data.d.results[0].FileLeafRef;
                alert(CalFileName);
            }       
        },
        error: function () {
            alert("Calendar not found");
        }
    });
}
</script>
  • Thanks LOVEZ, but I'm still getting an error Calendar not found. – Tamras Merin Dec 1 '17 at 18:01
  • Could the issue be because the section name has an ampersand (i.e. Cost & Billing Section)? Is there a way around it? I tried signing on as my test account and it worked since test account was assigned to another section (i.e. Customer Service Section) – Tamras Merin Dec 1 '17 at 22:43
  • Please provide the type of the fields and the issue data of the list. – LZ_MSFT Dec 4 '17 at 1:23
  • In the Site Pages library, I added 3 columns - all single line of text (Division, Branch, Section). However, in the Staff Directory list, they are all choice columns (Division, Branch, Section). Basically, when the current user clicks the promoted link tile, it should open the appropriate site page depending on his/her section. If this works, I also plan to do the same to dynamically open a link based on current user's division and branch. – Tamras Merin Dec 4 '17 at 17:06
  • If the Site Pages library is the SharePoint default library, please modify the REST URL "/_api/web/Lists/getByTitle('SitePages')/Items" to "/_api/web/Lists/getByTitle('Site Pages')/Items". – LZ_MSFT Dec 6 '17 at 1:50

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.