Saturday, August 24, 2013

Hide Upload,Multiple, Add Document link from library view for DocSet

The requirement is that to avoid user to create, edit and delete docset, but they can add, upload, edit document inside the Doset. Also, avoid user to add document outside the Docset. 

So for the above requirement, i had created css file and uplaod it to style library and add the path of file path on library view page, where docset available. The css available in my file is as below:-

TD.ms-addnew
{
DISPLAY: none !important
}
#Ribbon\.Documents\.New\.AddDocument-Large
{
display:none;
}
#Ribbon\.ListItem\.New\.NewListItem-Large
{
    display:none;
}
#Ribbon\.Documents\.New\.NewDocument-Large
{
   display:none;
}
#Ribbon\.Documents\.Manage\.EditProperties-Large
{
  display:none;
}
#Ribbon\.Documents\.Manage\.Delete
{
  display:none;
}
#Ribbon\.Documents\.Manage
{
  display:none;
}


and add the below javascript inside the doset page to hide the "View All Properties" and  "Edit All Properties".

<script language="javascript">
_spBodyOnLoadFunctionNames.push("HideShow");
 function HideShow()
{

   var EditProperties = document.getElementById ('EditPropsLink');
if(EditProperties != null)
{
   EditProperties.style.display = "none";
}
var ViewProperties = document.getElementById('ViewPropsLink');
if(ViewProperties != null)
{
 ViewProperties.style.display = "none";
}


}
<script/>
Happy coding..... :-)

Expand only the first group and have the rest of the groups collapsed in Dataform webpart SP 2010

Now, it is possible in DVWP to expand only the first group and have the rest of the groups collapsed.
Example:-
- Group: Year 2013
- Item 1
- Item 2
- Item 3
+ Group: Year 2012
+ Group: Year 2011
+ Group: Year 2010

Use the following steps:-

1) Create dataform webpart using Sharepoint designer and bydefault group is collapsed in dataform webpart.
2) Save the page and search webpart id for dataform webpart in view source of page. E.g In my case it is  MSOZoneCell_WebPartWPQ4. Search the id for div available in webpart, <div id="WebPartWPQ4".
3) Use the following script on page and replaced the WebPartWPQ4   with your dataform webpart div id.
<script src="https://Server/SiteName/Style%20Library/Custom%20Javascript/jquery-1.8.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
functioncollapseGroups() {
var someimage = document.getElementById('WebPartWPQ4');     //Webpart Id available in div tag
var myimg = someimage.getElementsByTagName('img')[0];     //  It will check first group image(+) available in div tag
var mysrc = myimg.src;
   //alert(mysrc);
    //$("img[src='/_layouts/images/plus.gif']:visible").parent().click();
    $(myimg).parent().click();
}
_spBodyOnLoadFunctionNames.push("collapseGroups");
</script>

The script will find 1st + sign image available next to group first and forcefully fired the click event.

Bingo!!!!, the script is work properly and our issue resolved.

Wednesday, July 17, 2013

Query string value in lookup field in NewForm of list/ libraries.

If you created custom action on list and want to pass query string value in lookup field  and removed the other option from lookup so user cant change the value of lookup field used the follwoing javascript.

e.g. You've built a nice custom page which lists your parent items and you have links to each parent items to add child items with respect to parent item. Also when user open the child item form user not allow to select other parent id from lookup column add the following script on child item form.

your links looks like:-

http://ServerName/Sites/SiteName/Lists/Listname/NewForm.aspx?ItemId ={ItemId}.

// Javascript for Querystring values selected in Lookup dropdown and removed others values on Form.
//Description:  If Lookup column value items less than 20, html tag of dropdown 'select'. If the items greater than 20 items, html tag automaticaaly converted into 'input' in sharepoint 2010. So this script work for both condition.


//looks for the control of the requested type with a title that matches our current field
function getField(typeOfField,fieldDisplayTitle)
{  
   var matchingDocs = document.getElementsByTagName(typeOfField);  
   for (var i=0; i < matchingDocs.length; i++)
   {  
     if (matchingDocs[i].title == fieldDisplayTitle)
     {  
       return matchingDocs[i];  
     }  
   }  
   return false;  
}   
   
function getParameterByName(name)
{
   name = name.replace(/[\[]/, "
\\\[").replace(/[\]]/, "\\\]");  
   var regexS =  name + "=([^&#]*)";  
   var regex = new RegExp(regexS);  
   var results = regex.exec(window.location.href);
   if(results == null)    
   return "";  
   else    
   return decodeURIComponent(results[1].replace(/\+/g, " "));
}
  
//Set query string value to lookup field when lookup items less than 20 and removed other values from dropdown.
function setSelectValue(lookupControl, valueToSet)
{
   for(var i = 0; i < lookupControl.length; i++)
   {
     if(lookupControl[i].value == valueToSet)
     {
       lookupControl[i].selected = true;
        //lookupControl.disabled=true;
     }
   }
   //to remove other option from the field
   var options = lookupControl.options;
   var iCount = 0;
   while (lookupControl.length > 1 )
   {
     if(options[iCount].selected != true)
     {
       options[iCount] = null;
     }
     else
     {
      iCount = iCount + 1
     }
   }
                   
}
  
//function to be registered on SP Body On Load to attempt to select the corresponding lookup field information
function attemptToSetField()
{
  var fieldNameToBeAutoSet = "File ID";  //This is the display name of the field you want to auto-set
  var queryStringParameterName = "ItemId";  //This is the query string key name you will be retrieving the ID from
  var idToBeSet = getParameterByName(queryStringParameterName);
  if(idToBeSet)//only run the auto-set if it's been passed to us via query string
  {
    var lookupControl = getField('select',fieldNameToBeAutoSet);
    if(lookupControl)
    {
      setSelectValue(lookupControl, idToBeSet);
    }
    else
    {
       lookupControl = getField('input', fieldNameToBeAutoSet);
       if(false === lookupControl)
       {
          alert('Unable to get dropdown named ' + fieldNameToBeAutoSet);

       }
       else
       {
          //Set query string value to lookup filed when lookup items more than 20 and removed other values from dropdown.
          lookupControl.setAttribute('choices',idToBeSet + '|' + idToBeSet);
          lookupControl.value = idToBeSet;
          lookupControl.selected = true;

       }
     
                    
    }
  }
}
  _spBodyOnLoadFunctionNames.push('attemptToSetField');

“Failed to get value of the “Attachments” column from the “Attachments” field type control. See details in log. Exception message: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)”

The attachment issue comes after customization of NewForm.aspx/EditForm.aspx page. After Customizing page even you have not attached any attachement the following error message showing on click of save button:-
“Failed to get value of the “Attachments” column from the “Attachments” field type control. See details in log. Exception message: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)”

Resolution:-
Disabled attachment option from list
Go to list --> settings --> advance settings ---> disable attachments.

OR

If you want the attachment column to be present, open page in sharepoint designer and findout the below tag and replace it....

<TemplateName xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">ListForm</TemplateName>

To

<TemplateName xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">ListForm2</TemplateName>

Friday, July 5, 2013

SharePoint Lookup field: - Behaves differently when items greater than 20

SharePoint Lookup field: - Behaves differently when items greater than 20
Lookup column having less than 20 items:
If your lookup column having 20 or less items, the lookup column rendered like a normal dropdown filed (i.e. if you checked the source code of the page, you will get HTML tag <Select title=”File ID”…..) as shown in below images.
1.       The below image showing no. of items less than 20
2.       The below image showing source code for the page

Lookup column having more than 20 items:
If your lookup column is having more than 20 items, the source code automatically changed and complicated in IE browser. In FireFox and other browsers the lookup column with more than 20 items still gets same HTML source i.e. <Select tag .  The images are shown in below:
1.       The below image showing no. of items greater than 20
2.       The below image showing source code for the page
When lookup column has more than 20 items in IE it gets generated INPUT text box with an image. The input field on the other hand having lots of event handler like onfocusout, onkeypress, onkeydown. One of the attributes of input tag is also “Choices” which contains all the ids and values of lookup separated by “|” character. The image next to Input field has an “Onclick” event that triggers the select and selected value pass to the attribute “Value”.

Tuesday, April 30, 2013

Remove hyperlink from lookup column in Dataview webpart in Sharepoint 2010

When you used lookup column in the list and implemented it in custom dataview webpart, the hyperlink appears on the lookup field value.

To remove the link from lookup field used the following tag:-

Just you have to replace with

<xsl:value-of disable-output-escaping="yes" select="@Teams"/>

By:

<xsl:value-of disable-output-escaping="yes"
select="substring-after(substring-before(substring-after(@Teams, 'ID='), '&lt;'), '&gt;')"/>


If you have added multiple selection lookup value in your list, and you want to remove hyperlink from field, the above trick will not work. For that we required to more customise the XSLT and used the following code:-

In multiple selection lookup value shows in data view web part:-

Team1; Team2; Team3; Team4

We will format it to:-

Team1
Team2
Team3
Team4

Using the following code:-

Add the below template to your data view web part:-

<!—Semicolon delimit start -- >
<xsl:template name="string-replace-all">
<xsl:param name="text" />
<xsl:param name="replace" />
<xsl:param name="by" />
<xsl:choose>
<xsl:when test="contains($text, $replace)">
<xsl:value-of select="substring-before($text,$replace)" />                
<xsl:value-of select="$by" />
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="substring-after($text,$replace)" />
<xsl:with-param name="replace" select="$replace" />
<xsl:with-param name="by" select="$by" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!—Semicolon delimit End -- >

<!—Remove Hyperlink start-- >

<xsl:template name="deleteAnchorTags">
<xsl:param name="Anchor"/>
<xsl:choose>
<xsl:when test="contains($Anchor, '&lt;')">                                    
<xsl:value-of select="substring-before($Anchor, '&lt;')"/>
<xsl:call-template name="deleteAnchorTags">
<xsl:with-param name="Anchor" select="substring-after($Anchor, '&gt;')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$Anchor"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!—Remove Hyperlink End-- >


And replace your tag with:-

<xsl:value-of disable-output-escaping="yes" select="@Teams"/>

By:-

<xsl:variable name="onlyText">
<xsl:call-template name="deleteAnchorTags">
<xsl:with-param name="Anchor" select="@Teams" />
</xsl:call-template>
</xsl:variable>
 <xsl:variable name="thisRow">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="$onlyText" />
<xsl:with-param name="replace" select="';'" />
<xsl:with-param name="by" select="'&lt;br/&gt;'" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$thisRow" disable-output-escaping="yes"/>


Happy coding..... :-) :-) :-)

Thursday, January 17, 2013

Ribbon menu missing on page load after customisation


After  Customisation in sharepoint pages ribbon menu missing on page load for perticular webpart.

            In sharepoint 2010, if you have more than one webpart on webpart page, the ribbon disappears because SharePoint doesn't know which web part to display ribbon for. This is by design because if you have a document library web part and list web part on the same page, the ribbons are obviously differ for the two and sharepoint needs to know which webpart to display the ribbon for.

To avoid the above problem use the following javascript.

<Script>
setTimeout(function()
{

var obj = document.getElementById("MSOZoneCell_WebPartWPQ3");
if(obj != null)
{

var obj1 = new Array()

obj1["target"] = obj

obj1 ["srcElement"] = obj

WpClick(obj1)
}
},2000)

</Script>

Overlay calendar items open in new window instead in Modal Popup

When you create overlay calendar view for combined 2 or 3 views especially for colour coded calendar, if you create event with category and click on it, it will open in popup which is by default function of SharePoint.
If you create event without category column value and click on it, it will open in new window. To remove the above problem use the below javascript.

<script src="/sites/CUEDemo/Code%20Library/jquery-1.8.3.min.js" type="text/javascript"></script>

<script type="text/javascript">
// load our function to the delayed load list
                _spBodyOnLoadFunctionNames.push('calendarEventLinkIntercept');

                // hook into the existing SharePoint calendar load function.
                function calendarEventLinkIntercept()
                {
                                if (SP.UI.ApplicationPages.CalendarNotify.$4a)
                                                {
                                                var OldCalendarNotify = SP.UI.ApplicationPages.CalendarNotify.$4a;
                                                SP.UI.ApplicationPages.CalendarNotify.$4a = function ()
                                                {
                                                OldCalendarNotify();
                                                bindEventClickHandler();
                                                }
                                                }
                                if (SP.UI.ApplicationPages.CalendarNotify.$4b)
                                                {
                                                var OldCalendarNotify = SP.UI.ApplicationPages.CalendarNotify.$4b;
                                                SP.UI.ApplicationPages.CalendarNotify.$4b =  function ()
                                                {
                                                OldCalendarNotify();
                                                bindEventClickHandler();

                                                } 
                                }
                  // future service pack change may go here!
                  // if (SP.UI.ApplicationPages.CalendarNotify.???)
                }

                function bindEventClickHandler()             {
                                $('.ms-acal-rootdiv a').click(function(){EditLink2(this,'WPQ2');return false;});
                }



</script>

Avoid Contributor user to edit home page in sharepoint 2012 Team site

This is contributor permission issue(sharepoint 2012 bug). Normally when you create Sharepoint 2012 team site, the home page by default create under "SitePages" library.

The contributor users on site can easily modify home page and change setting of webpart and layouts.
The key to that issue is in the description "customize Web Part Pages in document libraries ", and it's the "in document libraries " that's the problem. When a page is in a library, it is treated as a file or document. If the user has "contribute" or Edit permissions to the library storing the page then they have the right to edit the page.
In 2007 the home page (default.aspx) was stored outside of a library, so "contributors" did not have edit rights by default. In 2010 Team Sites the home page is stored in the SitePages library that inherits permissions from the site, and therefore contributors have edit rights.
The solution is to set custom permissions in the pages library. Just break inheritance and grant your team members View only rights and the Site Owners full control.

Happy Coding.......... :-)