1

I developed a Custom Form and at the form workflow I sent an email to the director to approve or reject user's request via link (to below script) that validated if the logged in user "username" is equal to the value of the field (Director username) of the form. If so, I wanna open the Director view and if not I wanna open Access Denied View.

Here's the verification script "client object model js":

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ Page Language="C#" %>
<%@ Register tagprefix="SharePoint" namespace="Microsoft.SharePoint.WebControls" assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta name="WebPartPageExpansion" content="full" />
<meta http-equiv="X-UA-Compatible" content="IE=10" />
<SharePoint:CssRegistration Name="default" runat="server"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
    <script>


       $(document).ready(function(){
            var scriptbase = "http://workspace.xxx.xx.xx/sites/IT_E-Services/SitePages/Home.aspx" + "/_layouts/15/";
        //Do not change the order, scripts should load by order.  
         $.getScript(scriptbase + "init.js",
                function () {
                    $.getScript(scriptbase + "SP.Runtime.js",
                       function () {
                    $.getScript(scriptbase + "SP.js", getViewByPermissions);
                  });
              });
               });

function getViewByPermissions() {
    var clientContext = new SP.ClientContext.get_current();
    var list = clientContext.get_web().get_lists().getByTitle("DepartmentDirector");
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<View>" +
                           "<Query>" +
                            "<Where>" +
                             "<Eq>" +
                              "<FieldRef Name='AccountId' />" +
                              "<Value Type='Text'>" + username + " </Value>" +
                             "</Eq>" +
                            "</Where>" +
                           "</Query>" +
                          "</View>");
    this.myList = list.getItems(camlQuery);
    clientContext.load(this.myList);
    clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed)
}

function onQuerySucceeded()
{
    var count = this.myList.get_count();
    if(count == undefined || count == 0)
       location.href = location.href = "http://workspace.xxx.xx.xx/sites/IT_E-Services/Lists/New%20User%20Account/Item/editifs.aspx?&DefaultView=Department Director View&List=4F573625-2865-4B86-85AA-E96A24BFE683&ID=[%CurrentItem:ID%]";//Access Denied View
    else //if(count > 0)
        location.href = "http://workspace.xxx.xx.xx/sites/IT_E-Services/Lists/New%20User%20Account/Item/editifs.aspx?&DefaultView=Access Denied View&List=4F573625-2865-4B86-85AA-E96A24BFE683&ID=[%CurrentItem:ID%]";//Director View
}

function onQueryFailed()
{
    location.href = "http://workspace.xxx.xx.xx/sites/IT_E-Services/Lists/New%20User%20Account/Item/editifs.aspx?&DefaultView=Department Director View&List=4F573625-2865-4B86-85AA-E96A24BFE683&ID=[%CurrentItem:ID%]";//Access Denied View
}

$(document).ready(function(){
   getViewByPermissions()
})
</script>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>

Problem is I'm getting following error: Uncaught ReferenceError: SP is not defined

How to fix it?

  • Try SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function() { // You code goes here }); – Atish Dipongkor - MVP Oct 20 '16 at 11:15
  • Still getting this error. Did you mean adding it inside JS tag ? – Learner Oct 20 '16 at 11:26
1

It's a normal behavior, Try to load the SP.js and SP.Runtime.js as the following :

The final Code should look like

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
    <script>


       $(document).ready(function(){
            var scriptbase = "http://yoursiteURL" + "/_layouts/15/";
        //Do not change the order, scripts should load by order.  
         $.getScript(scriptbase + "init.js",
                function () {
                    $.getScript(scriptbase + "SP.Runtime.js",
                       function () {
                    $.getScript(scriptbase + "SP.js", getViewByPermissions);
                  });
              });
               });
    // **add your code**


   function getViewByPermissions() {
    var clientContext = new SP.ClientContext.get_current();
    var list = clientContext.get_web().get_lists().getByTitle("DepartmentDirector");
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<View>" +
                           "<Query>" +
                            "<Where>" +
                             "<Eq>" +
                              "<FieldRef Name='AccountId' />" +
                              "<Value Type='Text'>" + username + " </Value>" +
                             "</Eq>" +
                            "</Where>" +
                           "</Query>" +
                          "</View>");
    this.myList = list.getItems(camlQuery);
    clientContext.load(this.myList);
    clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed)
}

function onQuerySucceeded()
{
    var count = this.myList.get_count();
    if(count == undefined || count == 0)
       location.href = location.href = "http://workspace.xxx.xxx.xx/sites/IT_E-Services/Lists/New%20User%20Account/Item/editifs.aspx?&DefaultView=Department Director View&List=4F573625-2865-4B86-85AA-E96A24BFE683&ID=[%CurrentItem:ID%]";//Access Denied View
    else //if(count > 0)
        location.href = "http://workspace.xxx.xxx.xx/sites/IT_E-Services/Lists/New%20User%20Account/Item/editifs.aspx?&DefaultView=Access Denied View&List=4F573625-2865-4B86-85AA-E96A24BFE683&ID=[%CurrentItem:ID%]";//Director View
}

function onQueryFailed()
{
    location.href = "http://workspace.xxx.xxx.xx/sites/IT_E-Services/Lists/New%20User%20Account/Item/editifs.aspx?&DefaultView=Department Director View&List=4F573625-2865-4B86-85AA-E96A24BFE683&ID=[%CurrentItem:ID%]";//Access Denied View
}
  • Still I'm getting nothing. I guess something wrong in my code itself – Learner Oct 19 '16 at 10:52
  • What should be in DepartmentDirector and AccountId ? – Learner Oct 19 '16 at 10:58
  • DepartmentDirector is the list name and account id is the field that you need to filter with it in your list , are you debug the solution via developer tools ? – Mohamed El-Qassas MVP Oct 19 '16 at 12:24
  • Department Director is field of group type from the active directory. I really don't know what to put in DepartmentDirector and AccountId ? – Learner Oct 20 '16 at 9:10
  • I'm gitting the follwoing errors: test.aspx:23 Uncaught ReferenceError: SP is not definedgetViewByPermissions @ test.aspx:23(anonymous function) @ test.aspx:56i @ jquery.min.js:2fireWith @ jquery.min.js:2ready @ jquery.min.js:2K @ jquery.min.js:2 jquery.min.js:4 GET yoursiteurl/_layouts/15/init.js?_=1476957575879 net::ERR_NAME_NOT_RESOLVED – Learner Oct 20 '16 at 10:01

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.