Conditionally hide Add 'New Item' link
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
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
- Nikita Kurguzov
- Posts: 889
- Joined: Mon Jul 03, 2017
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.
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
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
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
- Nikita Kurguzov
- Posts: 889
- Joined: Mon Jul 03, 2017
Dear Tony,
Ah, I see! In this case, I believe this code should help:
Let me know how it goes for you!
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();
Cheers
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: -
Thanks
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();
- Nikita Kurguzov
- Posts: 889
- Joined: Mon Jul 03, 2017
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:
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
- Nikita Kurguzov
- Posts: 889
- Joined: Mon Jul 03, 2017
Dear Tony,
No problem at all, you are very welcome!
No problem at all, you are very welcome!
Cheers
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
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
- Nikita Kurguzov
- Posts: 889
- Joined: Mon Jul 03, 2017
Dear Tony,
It would be easier to append a new link after hiding the existing one, like this:
After that, use some code to add functionality to it.
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>" );
Cheers
-
- Information
-
Who is online
Users browsing this forum: No registered users and 12 guests