0

I am looking at trying to run some code against a list object when a user is viewing it. (for example, when a user views an item to, on load, updating the item with a 'has been viewed' field to 'true')

My thought process is to run a piece of javasript to somehow, when the page loads with the item, to find the current item ID. Then, by using some other piece of code, to update the field automatically and save it.

I am just unsure how to effectively get the item ID using JS (Im relatively new to both SP and JS)

From what I can tell, the ID is in the url (in this case, it's '58') -

.../NewDisplayForm.aspx?ID=58&Source=...

What would be a suitable peice of JS to get this value when the page loads, and out of interest, what would be the suggested way to then programatically update the object field and save it?

thanks!

2

To get the querystring values, through JavaScript on a SharePoint site we can easily utilize the JSRequest object of SharePoint.

<script language="javascript">

//First we have to call the EnsureSetup method
JSRequest.EnsureSetup();
itemId = JSRequest.QueryString["ItemId"];

</script>

Reference from here

3

With JavaScript, you can get the current item id from variable _spPageContextInfo.pageItemId. Other spPageContextInfo variables are listed here.

However, I'm not sure if having the "has been viewed" tag on the actual item is the best approach.

Issue 1: Page is in edit mode, so you wouldn't be able to update the field when someone else views the latest major version of the page.

Issue 2: More rare issue might be when many users concurrently view the page and you need to update the field for all of them.

Issue 3: If versioning is enabled for the article, you will end up having very many versions, as I think you cannot do SystemUpdate (not increase version) when modifying item using JSOM.

Having said that, you could workaround issues 1 and 3 by having separate non-version-controlled list or lists where you have one column for article identifier (URL, list ID, or something else unique), and then another field where you store the "has been viewed" tag. If you want to tackle issue 2, you would add one row for each user, but that'll mean you get lots of rows. I'd perhaps attempt with solution where the "has been viewed" column contains comma separated list of user IDs, unless you know there are tens of people hitting the article with the same second.

  • Thanks Jussi for your great answer. Just a couple more questions... I'm trying to run `console.log(_spPageContextInfo.pageItemId)' but it seems like, in my case, there is no PageItemId property available to me. (I am running this in my debug console while viewing a list item). Regarding the issues you raised, the page wont be in 'edit' mode but in display mode. Will this be a problem still? Also the website/portal wont be used by more than 2-3 people concurrently. – Simon May 7 '15 at 7:47
  • To add more context to what I'm doing, I have a List where I am creating items from incoming mail (from a document library that is configured to receive email via sharepoint). When an email is received, I create a new item in another list using the parsed information. I want to try and manage which of these emails have been 'read' or not. Would there maybe be another way to do this maybe? – Simon May 7 '15 at 7:49
  • In that case I'd just take the ID from querystring. I haven't used the JSRequest myself, but you can find JavaScript functions allover the net that parse querystring. If you need to track how different users have read the generated email list item, and if you would store that information on the item, you would still need to edit the item programmatically and may run into issues I listed above. – Jussi Palo May 7 '15 at 10:19
  • Thanks Jussi. The answered I marked actually did the job, but I will follow your advice when it comes to actually trying to manage editing the object. Thanks for your assistance! – Simon May 7 '15 at 13:11

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.