|
Hi, Is there a way to hide the attachments field on the display and edit form depending on which user is logged in? Thanks. |
| ff4930 Friday, October 16, 2009 8:08 PM |
var elm = document.getElementById("idAttachmentsRow");
if(elm){
elm.style.display='none'
}
Getting Current User from javascript
Regards
Prasad - Unmarked As Answer byff4930 Tuesday, October 20, 2009 4:33 PM
- Marked As Answer byff4930 Tuesday, October 20, 2009 2:27 PM
- Marked As Answer byCharlie WuModeratorWednesday, October 21, 2009 2:42 AM
-
|
| Guru Karnik Saturday, October 17, 2009 2:19 AM |
You could use JavaScript to hide the following Attach File link HTML elements:
<a href="javascript:UploadAttachment()" onclick="javascript:UploadAttachment();" accesskey="I" title="Attach File" class='ms-toolbar'><img align='absmiddle' alt="Attach File" src="/_layouts/images/attachtb.gif" style='border-width:0px;' width='16' height='16'></a>
This JavaScript could be contained in a Content Editor web part which is audience-targed for the roles you wish to surpress the Attach File link for. Steven |
| Steven Fischer Saturday, October 17, 2009 12:40 AM |
var elm = document.getElementById("idAttachmentsRow");
if(elm){
elm.style.display='none'
}
Getting Current User from javascript
Regards
Prasad - Unmarked As Answer byff4930 Tuesday, October 20, 2009 4:33 PM
- Marked As Answer byff4930 Tuesday, October 20, 2009 2:27 PM
- Marked As Answer byCharlie WuModeratorWednesday, October 21, 2009 2:42 AM
-
|
| Guru Karnik Saturday, October 17, 2009 2:19 AM |
Hi ff4930,
the suggestions provided by the other persons are indeed useful but are maybe hard to use if you want to deploy these changes to all lists you want this behavior to occur. A display and edit form are always linked to a certain list and maybe you don't want to make/hookup a list for/to each particular list. Therefore I just want to add a quick hint which you can use to deploy this javascript to all edit forms in your site collection with little to no effort. Read this post on my blog:
http://microsoftsharepointandbeyond.blogspot.com/2009/10/customizing-ootb-sharepoint-forms.html
which will show you how to inject javascript on certain pages of your choice using an http module.
Greets,
Frank |
| Frank.Cleynen Saturday, October 17, 2009 10:53 PM |
I agree. But HttpModule will be an overhead since all sharepoint requests needs to go through it. This might be an overkill for just injecting javascript conditionally on to the pages.You could also add additional conditions in the javascript to enforce it only on the required Display/Edit forms of the list. The advantage here is that this will be processed at the client side in the browser.
E.g. if (window.location.href.match("/sites/abcd/lists/list1"))
{
....
}
Regards
Prasad |
| Guru Karnik Monday, October 19, 2009 4:50 PM |
It will indeed create an overhead but I doubt it will be really noticeable. The condition itself I myself put right into the HttpModule and not in the javascript. This implies there will be overhead only once (the httpmodule) and not twice (the httpmodule and the if statement in the javascript).
Greets,
Frank |
| Frank.Cleynen Monday, October 19, 2009 9:32 PM |
var
elm = document.getElementById("idAttachmentsRow"
);
if
(elm){
elm.style.display='none'
}
Getting Current User from javascript
Regards
Prasad
Hi Guru, This has worked well, I have 1 related question. On the Edit Form, how would I hide the 'Attach File' button via Javascript? Frank - This is specifically targeted to 1 list so the approach Guru mentioned is fine. Thank you for your answer. |
| ff4930 Tuesday, October 20, 2009 2:08 PM |
Hi, I managed to figure it out. Here is the solution if anyone needs it.
function HideButton()
{
var tags = document.getElementsByTagName('a');
for (var i=0; i < tags.length; i++)
{
if(tags[i].title == 'Attach File')
{
tags[i].style.display = 'none'
}
if(tags[i].id == 'ctl00_m_g_1b517923_1f1a_475a_8e10_57a050a1c53a_ctl00_ctl01_ctl00_toolBarTbl_RptControls_diidIOAttach_LinkText')
{
tags[i].style.display = 'none'
}
}
}
|
| ff4930 Tuesday, October 20, 2009 2:26 PM |
Any specific reason why you are looping through all <a> tags? You already have the id of the tag.
var elm = document.getElementById("ctl00_m_g_5aa876af_5d71_4868_a3bd_6834c6be78d8_ctl00_ctl01_ctl00_toolBarTbl_RptControls_diidIOAttach_LinkText");
if(elm){
//Hide the TR enclosing this TD
elm.parentNode.parentNode.style.display='none';
}
Regards Prasad |
| Guru Karnik Tuesday, October 20, 2009 3:13 PM |
Ok, I just tried the page using Mozilla and it is not hiding the attachment table. Can anyone help? This is the code that is working in IE for me.
<script type="text/javascript">
var currentUser = getCurrentUser();
if(currentUser != null)
{
if(currentUser == "company\\test1")
{
var elm = document.getElementById("idAttachmentsRow");
if(elm)
{
elm.style.display='none';
}
}
}
function getCurrentUser()
{
var tags = document.getElementsByTagName('a');
for (var i=0; i < tags.length; i++)
{
if(tags[i].innerText.substr(0,7) == 'Welcome')
{
return tags[i].innerText.substr(8,tags[i].innerText.length);
}
}
}
</script>
|
| ff4930 Tuesday, October 20, 2009 3:21 PM |