1

I have a pretty complex task I need to perform in Sharepoint, but I'm a total beginner when it comes to JavaScript!

Here's what I need to do: I have a list on my site that is basically a database of employees along with metadata. The list is updated automatically updated via a few different workflows I have running on the site. For training purposes, I need to keep track of how far these users have progressed through a curriculum part of our site. The curriculum is made up primarily of external links.

The solution I came up with would be to use columns in the user database to track the number of links in each lesson the user has clicked on. I'm a complete novice when it comes to JavaScript, so I've been researching everything I can find to do the following:

  1. Lookup the Username of the current user
  2. Compare that username to the username field in the Db to get the list Item ID
  3. Run a script when the user clicks on a hyperlink to update their corresponding list item in the user Db with the number of links they have clicked in a particular lesson.

There may be another way to do all this, and I'm open to suggestions. Like I said, I'm new to Javascript, but I've performed some miracles in SharePoint using Lists, Libraries, Forms, Workflows, etc.

Here's the code I've scrapped together from various articles. I'm assuming theres some pretty horrendous mistakes:

    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2014.02/jquery.SPServices.min.js">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
    function GetUserName()
{
    var wshell = new ActiveXObject("WScript.Shell");
    var username = wshell.ExpandEnvironmentStrings("%USERNAME%");
    }
    var queryLookup = "<Query><Where><Eq><FieldRef Name='PinExtract'/><Value Type='Text'>"+username +"</Value></Eq></Where></Query>";
    var itemID="";
    $().SPServices({
operation: "GetListItems",
async: false,
listName: "UserList",
CAMLViewFields: "<ViewFields><FieldRef Name='ID'/></ViewFields>",
CAMLQuery: queryLookup,
completefunc: function(xData,Status) { 
    itemID = $(xData.responseXML.xml).find("z\\:row").attr("ows_ID");
  } 
       });

    $(document).ready(function() {

    var title=username;

    UpdateListItem(title);

    });

    function UpdateListItem(title)

    {

    $().SPServices({

    operation:"UpdateListItems",

    async: false,

    batchCmd: "Update",

    listName: "UserList",

    ID: itemID,

    valuepairs:[["JSTestField",title]],

    completefunc:function(xData, Status)

    {

    alert("Item Updated");

    }

    });

    }


    </script>

To (possibly) make things more complicated, our pages are SP 2007, and I am not a site collection administrator. I only have control over our very small subsite, way down the line of our huge organization.

  • Rather than ask for the entire solution all in one shot, break your task into smaller subtasks, and solve each one separately, and if necessary, ask a focused question with a code sample that illustrates the specific problem at hand. This will increase the chances that someone here will take a crack at it. Free tip - your script tags should always be protocol-agnostic. Your first one is, but your second one is not. – Derek Gusoff Aug 27 '15 at 13:51
  • Thank you for the tip Derek! I'm new here. The code above seems to all work independently, just not together. I'll break each piece out though and see if some insight. – JSNewbie Aug 27 '15 at 17:28

Your Answer

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

Browse other questions tagged or ask your own question.