Saturday, August 6, 2011

Hide the “People and Group” section from breadcrumb in userdisp.aspx

If you want to hide any breadcrumb in sharepoint application pages, Then you are at the right spot. This can be done via javascript.

But Microsoft will not support/recommend modifying the OOTB application pages. 
So we can tweak the application page with a word of caution. It is entirely upto you to decide whether to follow it or not.

Here is the code for reference.

I'm hiding the "People and Group” in userdisp.aspx for those users from a different domain.

<input type="hidden" value="<%=Request.ServerVariables["Auth_User"]%>" id="user_name">

<script language="javascript">
var username=document.getElementById("user_name").value;
     alert(username);
   var domainname =username.substring(0,username.indexOf("\\"));
    alert(domainname.toLowerCase());

if(domainname.toLowerCase()=="Domain_name")//Please give Domain_name in lower case
{
var child=document.getElementById("ctl00_PlaceHolderTitleBreadcrumb_ContentMap").childNodes;

for(i=0;i<child.length;i++)
{
 if(child[i].hasChildNodes);
{
    var spantag=child[i].childNodes;
   
    for(j=0;j<spantag.length;j++)
    {
        if(spantag[j].tagName=="A")
        {
         var anchtag=spantag[j];
         hrefattrb=anchtag.getAttribute("href");
        
         if(hrefattrb.indexOf("people.aspx")!=-1)
         {
         anchtag.style.display="none";
         child[i].style.display="none";
         child[i+1].style.display="none";
         }
        
        }
    }
}
}
}
<script>

How to change rows to columns in SQL


There are two ways(as per my knowledge) to change rows to columns in SQL

1.Pivot statements
2.Case statements

Case statements
 
If the column names are static, it is quite easy to write.i.e For example say months in a year..
we are completely aware of those colmns names it could only be between jan-dec. In this case we can go ahead and use "Case" directly.

In the below snippet I'm changing one column values to column headers depending upon the month and year.

Select [year],
sum(case when [month]='Jan' then 1 else 0) as 'Jan',
sum(case when [month]='Feb' then 1 else 0) as 'Feb',
sum(case when [month]='Mar' then 1 else 0) as 'Mar',
sum(case when [month]='Apr' then 1 else 0) as 'Apr',
sum(case when [month]='May' then 1 else 0) as 'May',
sum(case when [month]='Jun' then 1 else 0) as 'Jun',
sum(case when [month]='Jul' then 1 else 0) as 'Jul',
sum(case when [month]='Aug' then 1 else 0) as 'Aug',
sum(case when [month]='Sep' then 1 else 0) as 'Sep',
sum(case when [month]='Oct' then 1 else 0) as 'Oct',
sum(case when [month]='Nov' then 1 else 0) as 'Nov',
sum(case when [month]='Dec' then 1 else 0) as 'Dec'        
from table_name
group by [year] 


If the column names are dynamic, we can the same logic above using cursors.
But please do not cursors until there is no other way.

declare @strSQL varchar(4000)
set @strSQL='select [Column_To_group
] '
Declare @name varchar(100)
DECLARE descrcursor CURSOR FOR
 
select dept_name from dbo.tblDepartment /* I have the column names in a table*/


OPEN descrcursor

FETCH NEXT FROM descrcursor
INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
set @strSQL= @strSQL + ' ,sum(case when department='''+replace(@name,'''','''''')+''' then 1 else 0 end ) as ['+@name+']'
FETCH NEXT FROM descrcursor
INTO @name
End
CLOSE descrcursor
DEALLOCATE descrcursor

set @strSQL = @strSQL + ' from table_name
group by [Column_To_group] '
print @strSQL
exec (@strSQL)

PIVOT

The other way is through using PIVOT.
I'm working on this sample, will update it soon.

Sharepoint Document library - Access denied while reading draft

In your custom code, you might have written a logic to read all documents from a document library. Sometimes you might get access denied for some files in library.

The reason behind this is the version setting of the document library, especially when you have "major & minor" versioning setting enabled. When you track major and minor versions, the major versions are whole numbers, and the minor versions are decimals. Minor versions are the draft versions, major versions are the published ones.
For example, 0.1 is the first minor version of a file, 1.4 is the fourth minor version of a file that was published once, and 3.0 is the third major version of a published file.


The version is stored by default is a minor version. When you save a file and close it, the version is tracked as a minor version. Minor versions are considered drafts that are still being developed.
You must first publish the file in order for it to become a major version. You can publish the file by using drop-down commands in a library

In your API, if lock a sitecollection and then try to read your draft document, obviously you will get "access denied", meaning, the content DB is in read only mode when the site collection is locked


The reason behind this was that, documents were never published [they were in draft mode only]. So documents have to be published with at least one major version to prevent this error.