Conditionally hide Add 'New Item' link

Discussions about Cross-site Lookup
Locked
User avatar
TonyDuke
Posts: 71
Joined: Mon Sep 11, 2017

04 May 2018

Good Afternoon,

We are using Cross site extensively across our site and have a number of lists where we would like to have the ability to use the Add 'New Item' link however we would only want this to appear either on certain form sets or for certain users, is there a way of conditionally hiding the hyperlink?

Thanks

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

04 May 2018

Dear Tony,
Are you using SharePoint Online or SharePoint On-Premises? Hiding the link shouldn't be an issue with JavaScript, but checking user's group and other data is trickier. It might be easier to just use Form Sets instead.
Cheers

User avatar
TonyDuke
Posts: 71
Joined: Mon Sep 11, 2017

10 May 2018

Hi Nikita,

We are using SharePoint Online, I have no issue with the script for checking user groups etc. we are already doing this across our site to conditionally show/hide fields and also set certain fields as read only, so its just the specific code to hide the link...

Thanks

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

10 May 2018

Dear Tony,
Ah, I see! In this case, I believe this code should help:

Code: Select all

//hide:
fd.field('CSLookup').control()._el().find("a:contains('Add new item')").hide();
//show:
fd.field('CSLookup').control()._el().find("a:contains('Add new item')").show();
Let me know how it goes for you!
Cheers

User avatar
TonyDuke
Posts: 71
Joined: Mon Sep 11, 2017

11 May 2018

Hi Nikita,

This doesn't seem to work, the internal name of my field is 'Industry' so I am guessing I replace 'CSLookup' with this, I had also reworded the text to say 'Add new Industry' so tried this. I have also tried to reset the text back to the default 'Add new item' but it doesn't seem to hide.

the code I have added for now is: -

Code: Select all

fd.field('Industry').control()._el().find("a:contains('Add new item')").hide();
Thanks

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

11 May 2018

Dear Tony,
Can you tell me when the code is executed? If it is executed as soon as the form opens, the field has no time to load. Try to run the code from the console to check if it works at all. An interval timer can help check if the field has initialized already:

Code: Select all

function checkCrossSiteLink(){
	var link = fd.field('CSLookup').control()._el().find("a:contains('Add new item')")[0];
	if (link){
		$(link).hide();
		clearInterval(timerId);
	}
}

var timerId = setInterval(function() {
  checkCrossSiteLink();
}, 200);
Cheers

User avatar
TonyDuke
Posts: 71
Joined: Mon Sep 11, 2017

11 May 2018

Hi Nikita,

With the interval timer the functions works as required.

Thanks again...

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

11 May 2018

Dear Tony,
No problem at all, you are very welcome! :)
Cheers

User avatar
TonyDuke
Posts: 71
Joined: Mon Sep 11, 2017

17 May 2018

Hi Nikita,

One last thing on the Add New Item Link, when it is shown, I would like to hijack the onClick event and change the functionality such as opening a different form set, or adding data into the session store for use in the opened form.

As an Example, I have a Lookup called Address Name, with the Add New Item link, on click I would like to put the Company Name & ID into the Session Store so that on the New Form this can be set into a Lookup Field for the Company Name for the New Item created. The Reason for doing this is that throughout our site we have cascading lookups, where a Company Name is selected first and then Only Address Names related to that Company can be selected in the Address Name Lookup...

Thanks

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

17 May 2018

Dear Tony,
It would be easier to append a new link after hiding the existing one, like this:

Code: Select all

$(link).after( "<a class='my-link'>Test</a>" );
After that, use some code to add functionality to it.
Cheers

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 12 guests