Friday, September 21, 2012

Sorting Outline Numbers in SharePoint list using Calculated columns

Requirement: We have a column in a list named SNumber. The value for this is would be similar to outline numbers in Word documents.

1.1
1.2
1.3
1.10
1.1.1
1.1.2
1.2.3.3
etc

Problem: We were not able to sort the values, and the values are sorted in the following order. The SharePoint calculated field formulas are not strong enough to achieve this.

1.1
1.10
1.2
1.3
etc


Workaround: We have multiple options to achieve this, this can be done using Event Handlers. Here this is done using multiple calculated field.

The minimum and maximum value would be
1.0 to 99.99.99.99

The logic used to achieve this is to split and pad the values. For instance, converting the following value
1.10.2.3   to 01100203

01.10.02.03 (prefix '0' before each part separated by dot)
01100203 (and  merge all the parts)

For this, I've created 8 calculated columns, you just need to replace the field name SNumber with your field name.

Column Name
Formula
part1
=IF(LEN(LEFT(SNumber,INT(FIND(".",SNumber)-1)))
<2,"0"&LEFT(SNumber,INT(FIND(".",SNumber)-1)),
LEFT(SNumber,INT(FIND(".",SNumber)-1)))
section2
=RIGHT(SNumber,LEN(SNumber)-INT(FIND(".",SNumber)))
part2
=IF(ISNUMBER(INT(FIND(".",section2)-1)),
IF(LEN(LEFT(section2,INT(FIND(".",section2)-1)))<2,
"0"&LEFT(section2,INT(FIND(".",section2)-1)),
LEFT(section2,INT(FIND(".",section2)-1))),
IF(LEN(section2)<2,0&section2,section2))
section3
=RIGHT(section2,LEN(section2)-IF(ISNUMBER(INT(FIND(".",section2))),
INT(FIND(".",section2)),LEN(section2)))
part3
=IF(ISNUMBER(INT(FIND(".",section3)-1)),
IF(LEN(LEFT(section3,INT(FIND(".",section3)-1)))<2,
"0"&LEFT(section3,INT(FIND(".",section3)-1)),
LEFT(section3,INT(FIND(".",section3)-1))),IF(LEN(section3)=0,"00",
IF(LEN(section3)<2,"0"&section3,section3)))
section4
=RIGHT(section3,LEN(section3)-IF(ISNUMBER(INT(FIND(".",section3))),
INT(FIND(".",section3)),LEN(section3)))
part4
=IF(ISNUMBER(INT(FIND(".",section4)-1)),IF(LEN(LEFT(section4,
INT(FIND(".",section4)-1)))<2 amp="" div="" section4="">
LEFT(section4,INT(FIND(".",section4)-1))),IF(LEN(section4)=0,"00",IF(LEN(section4)<2,"0"&section4,section4)))
customSNumber
 =IF(ISNUMBER(part1&part2&part3&part4),part1&part2&part3&part4,0)

Now, just sort the list by customSNumber column

Monday, June 25, 2012

How to replace single quote in XSLT

To replace a single quote in an XSLT view of Dataform webpart, the following solution is the easiest.

The condition we wanted to check is if the column Resolution is equal to Won't Fix, since there was a single quote in the condition, the web part was failing to display. So the condition is changed like below to replace the single quote with white space.


 <xsl:value-of select="count(/dsQueryResponse/Rows/Row[translate(@Resolution, &quot;'&quot;,'') = 'Wont Fix'])"/>

Thursday, June 21, 2012

Apply borders to the XSLT Data View using SharePoint Designer

The Dataview webpart is customized by converting it to XSLT Data View.
To apply border for the XSLT Data View table using SharePoint Designer. 

In Designer, locate the table tag in the Web Part source view

<TABLE ID="{$List}-{$View}" Summary="{$List}" xmlns:o="urn:schemas-microsoft-com:office:office" o:WebQuerySourceHref="{$HttpPath}&amp;XMLDATA=1&amp;RowLimit=0&amp;View={$View}" width="100%" class="ms-listviewtable" border="0" 

Update the border="1" and also other changes based on your requirement, refer to the following post for more information about the table styles

http://www.htmlcodetutorial.com/tables/index_famsupp_147.html

If you applying the border for both rows and column, you'll see the border missing in the empty fields. You need to replace the empty fields with white-space.
"&nbsp;" will not work in XSLT, instead you can use &#160; 

Wednesday, June 13, 2012

SharePoint Server Error in '/' Application. The resource cannot be found.


I was getting the following error when trying to access the SharePoint site.

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /sites/companyname/default.aspx



Solution:

My SharePoint site was running in port 80. In IIS, I noticed that the Default Web Site, that is supposed to be stopped, was running. I stopped the Default Web site and the SharePoint site is up and running

Thursday, May 24, 2012

List all site owners in a site collection programmatically



The following code is a simple code to get all site owners from the site collection. Execute the following code as a console application

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.IO;

namespace MyPortal
{
    class ListAllSiteOwners
    {
     
        static void Main(string[] args)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {

                FileStream ostrm;
                StreamWriter writer;
                TextWriter oldOut = Console.Out;
                try
                {
                    ostrm = new FileStream("./SiteOwners.txt", FileMode.OpenOrCreate, FileAccess.Write);
                    writer = new StreamWriter(ostrm);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Cannot open  SiteOwners.txt for writing");
                    Console.WriteLine(e.Message);
                    return;
                }
                Console.SetOut(writer);

             
                using (SPSite site = new SPSite(args[0]))
             
                    foreach(SPWeb web in site.AllWebs)
                    {
                        //SPUserCollection allAdmins = web.AssociatedOwnerGroup;
                        SPGroup ownerGroup = web.AssociatedOwnerGroup;
                        if (ownerGroup != null)
                        {

                            foreach (SPUser user in ownerGroup.Users)
                            {
                                Console.WriteLine(web.Url +"\t"+ ownerGroup.Name + "\t" + user.Name);
                            }
                        }
                        web.Dispose();
                    }
             
                Console.SetOut(oldOut);
                writer.Close();
                ostrm.Close();
            });
        }
    }
}

After the code is built, you need to execute the  ListAllSiteOwners.exe from the command prompt with site collection URL as argument. 

D:\> ListAllSiteOwners http://yoursitecollectionURL

You will find the  SiteOwners.txt  file in the same location where the exe is placed. Copy the content from the text to excel for better view.

Thursday, March 29, 2012

Programmatically add a blank row at the end of a InfoPath repeating table

Code snippet to add a blank row as last row in a InfoPath repeating table. 

//Get the repeating table node
XPathNavigator Group = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:AnalysisGroup/my:AnalysisTable", NamespaceManager);
//Get the Iterator object to iterate the node to last row
XPathNodeIterator rows = Group.Select("/my:myFields/my:AnalysisGroup/my:AnalysisTable", NamespaceManager);
                      
//Loop to move the cursor to last row
for (int counter = 0; counter < rows.Count; counter++, rows.MoveNext()) ;
//clone the last row and insert it after that
rows.Current.Clone();
rows.Current.InsertAfter(rows.Current);
//move the cursor to the last row
rows.MoveNext();
                      
//Set all the fields in the row to blank
rows.Current.SelectSingleNode("my:ItemDesc", NamespaceManager).SetValue("");
rows.Current.SelectSingleNode("my:Tax", NamespaceManager).SetValue("");

Friday, March 16, 2012

Print a Web Part Zone

Requirement: To print multiple Web Parts in a Web Part zone from the home page of a Web site.

Following are the steps to achieve this

1. Add the Web Parts in a Web Part Zone

2. Open the site in the SharePoint Designer, open the home page aspx file, select the Web Part Zone that you want to take the print out in the design view,  and wrap the Web Part zone by adding a <div> tag,


<div id="WebPartZone1">
<table width="100%"  cellpadding=0 cellspacing=0 style="padding: 5px 10px 10px 10px;">
 <tr>
  <td valign="top" width="70%" colspan="4">
  <WebPartPages:WebPartZone .................
                                    ..................................
                                      ..............................
                              </WebPartPages:WebPartZone>
                    </td>
                    </tr>
                     </table>
</div>


3. Add a Content Editor Web Part in the Zone and add the following script code in the source




<style type="text/css">
.styled-button-2 {
-webkit-box-shadow: rgba(0, 0, 0, 0.0976562) 0px 1px 0px 0px;
background-color: #EEE;
border: 1px solid #999;
color: #666;
font-family: 'Lucida Grande', Tahoma, Verdana, Arial, Sans-serif;
font-size: 11px;
font-weight: bold;
padding: 2px 6px;
height: 28px;
}
</style>
<div id='printButton' align="right"><input type="button" class="styled-button-2" OnClick="javascript:void(PrintWebPart())" value="Print Dashboard"></div>

<script language="JavaScript">
//Controls which Web Part or zone to print
var WebPartElementID = "WebPartZone1";

//Function to print Web Part
function PrintWebPart()
{
var bolWebPartFound = false;
if (document.getElementById != null)
{
//Create html to print in new window
var PrintingHTML = '<HTML>\n<HEAD>\n';
//Take data from Head Tag
if (document.getElementsByTagName != null)
   {
var prButton = document.getElementById('printButton');
        prButton.style.display = 'none';
   var HeadData= document.getElementsByTagName("HEAD");
   if (HeadData.length > 0)
    PrintingHTML += HeadData[0].innerHTML;
   }
PrintingHTML += '\n</HEAD>\n<BODY>\n';
var WebPartData = document.getElementById(WebPartElementID);
if (WebPartData != null)
{
   PrintingHTML += WebPartData.innerHTML;
   bolWebPartFound = true;
}
else
{
   bolWebPartFound = false;
   alert ('Cannot Find Web Part');
}
}
PrintingHTML += '\n</BODY>\n</HTML>';
//Open new window to print
if (bolWebPartFound)
{
var PrintingWindow = window.open("","PrintWebPart", "toolbar,width=800,height=600,scrollbars,resizable,menubar");
PrintingWindow.document.open();
PrintingWindow.document.write(PrintingHTML);
// Open Print Window
PrintingWindow.print();
}
prButton.style.display = '';
}
</script>