Friday, 16 May 2014

Customize the SmallSearchInputBox

Sometimes you need to customize the OTB searchbox but without touching control templates or building out your own custom webpart control. This restriction commonly comes from environments in which you are restricted to just SharePoint Designer. To resolve this I would utilize JavaScript and CSS to just customize the look and feel of the SearchBox. the following is a native JS implementation to change the placeholder text and to rewire the the onblur event to use a new function that would reset the placeholder text to what we specified.

var spm= spm|| {};

if (typeof spm.searchBoxExtension == 'undefined' || spm.searchBoxExtension == null)
{
  spm.searchBoxExtension = {

    init: function() {

      var searchBox = document.getElementsByName("InputKeywords")[0];
      searchBox.value = "What are you searching for?";
      searchBox.onblur = this.customBlur;
    },

    customBlur: function(event) {
      if (this.value == '') {
        this.value = 'What are you searching for?';

        if (this.className.indexOf('s4-searchbox-QueryPrompt') == -1)
          this.className += this.className ? ' s4-searchbox-QueryPrompt' : 's4-searchbox-QueryPrompt';

        document.getElementById('ctl00_PlaceHolderSearchArea_ctl01_ctl04').value = '0'
      } else {
        document.getElementById('ctl00_PlaceHolderSearchArea_ctl01_ctl04').value = '1';

      }
    }
  }
}

(function(){
  _spBodyOnLoadFunctionNames.push("spm.searchBoxExtension.init");
})()

Thursday, 8 May 2014

List All site Collections

This will list all the web application and some details about their site collections

Clear-Host
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

function GetSiteDetails()
{
    foreach ($site in Get-SPWebApplication | Get-SPSite)
    {
        $data = @{"Url" = $site.url
                  "Root Web" = $site.rootweb
                  "Template" = $site.rootweb.WebTemplate
                   "Web Application" = $site.webApplication
                  }
        New-Object PSObject -Property $data
    }
}


GetSiteDetails | Out-GridView

Friday, 2 May 2014

Upload file to Document Library

Sometimes, you want to quickly upload a file to a document library, especially when you're using a sandboxed solution and you can't store your javascript or StyleSheet Files in the Layouts folder, long ago I mentioned that you could create a batch file that would xcopy your files into the layouts folder hastening the develop/debug cycle, well this is the same thing for Sandbox solutions.

function LoadSharePointPowerShellEnviroment
{
    write-host
    write-host "Setting up Powershell enviroment for Sharepoint" -foregroundcolor Magenta
    Add-PSSnapin "Microsoft.Sharepoint.PowerShell" -ErrorAction SilentlyContinue
    Write-host "Sharepoint PowerShell Snapin loaded." -foregroundcolor Green
}

Clear-Host
LoadSharePointPowerShellEnviroment
write-host


Clear-Host
# Set the variables
$WebURL =  "https://stiteCollection/sites/Marks"
$DocLibName = "SiteAssets"
$FilePath = "C:\Users\SPDevFAdmin\Documents\Scripts\AppLoader.js"


$web = Get-SPWeb $WebURL
$List = $web.GetFolder($DocLibName)
$Files = $List.Files

# Get just the name of the file from the whole path
$FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1)

# Load the file into a variable
$File= Get-ChildItem $FilePath


Write-Host "uploading file $File " -ForegroundColor Yellow
$stream = $file.OpenRead()
     

# Upload it to SharePoint
$done = $Files.Add($DocLibName +"/" + $FileName, $stream,$true)

Write-Host "$done has been uploaded" -ForegroundColor Magenta

if($stream){
    Write-Host "dispose stream" -ForegroundColor Yellow
    $stream.Dispose()
}

if($web -ne $null){
    Write-Host "dispose web" -ForegroundColor Yellow
    $web.Dispose();
}

Wednesday, 16 April 2014

Duplicate web parts on page

So you've created a page layout or a site page that has a webpart deployed to the page through the Elements.xml file in the module that contains your page(s) and now every time you deploy the module you get duplicate web parts, one for every deploy... rather annoying.

to get around this you need to write some code in your event receiver to either iterate over all the webparts on the page delete them and just add yours back in, or iterate over all of the webparts and remove all but the first one.

public void RemoveExtraWebParts(SPSite site)
{
    //Make sure its actually a site
    if (site != null)
    {
        //get the sites root web
        var web = site.RootWeb;

        //Grab the root webs pages gallery
        var pages = web.GetList("Pages");

        //Itterate through all pages
        foreach (SPListItem li in pages.Items)
        {
            //create a check for pages that end in 401.aspx or 404.aspx
            var r = new Regex(@".40[1,4]\.aspx$");

            //Check if page url matches above regex
            if (r.IsMatch(li.Url))
            {
                SPFile page = li.File;

                try
                {
                    //Check out the page if need be
                    if (page.RequiresCheckout)
                        page.CheckOut();

//move through all webparts on page, remove all but the first one(declared by us)
                    //but only if it's a Content Editor webpart
                    using (var mgr =
                        page.GetLimitedWebPartManager(
PersonalizationScope.Shared))
                        if (mgr.WebParts.Count > 1)
                            for (int i = mgr.WebParts.Count - 1; i > 0; i--)
                            {
                                var wp = mgr.WebParts[i];
                                if (wp.Title.Equals("Content Editor"))
                                    mgr.DeleteWebPart(mgr.WebParts[i]);
                            }

                    page.CheckIn("Checked in by feature reciever");
                    page.Approve("Approved By feature reciever");
                }
                catch (Exception)
                {
                    page.UndoCheckOut();
                }
            }
        }
    }

}

Monday, 14 April 2014

Finding a Site Id through web interface


If you don't have powershell or visual studio, but need to get the Web id of your site follow this procedure.

site actions->Content & structure

once there pick the site setting from the site you're interested in's context menu as shown below


once that's done you'll see it in the url


but for an easier time hit the browser back button on the site settings page and you'll get

Friday, 11 April 2014

Using SPWebConfigModification

The SPWebConfigModification class is used to as the name suggests: to modify the web config; basically to my understanding what it does is, stores a collection of web config modifications in the content database and then when the ApplyWebConfigModifications method is called the changes are propagated to all the web configs on a front-end web server. This is the preferred manner to update your web config.

The SPWebConfigModification class has a number of properties that need to be set:
  • Path: an xPath to where you want your modification to go
  • Name: a unique name saved in the content database (i think) and used by the SPWebConfigModification class to ensure that multiple entries of the modification do not occur.
  • Value: the actual web config modification
  • Type: What kind of modification:  SPWebConfigModification.SPWebConfigModificationType
    • EnsureChildNode
    • EnsureAttribute
    • EnsureSection
  • Owner: who owns the particular modification,
  • Sequence: set the order in which the modifications occur, i'd start 10 and incriment in 10s.

now in the class definition there's an example of how to hand code all the values for a safe control, but what i want to do is add an httpHandler and I want to use reflection so that I can have one function that would handle adding any Handler to my web config.

using reflection we can create a function that just extracts all the details like so

public SPWebConfigModification GetModule(Type ModuleType)
{
    Assembly ModuleAssembly = ModuleType.Assembly;
           
    string NameSpace = ModuleAssembly.GetTypes().Select(t => t.Namespace).FirstOrDefault();
    string ClassName = ModuleType.Name;
    string AssemblyName = ModuleAssembly.FullName;

    return new SPWebConfigModification
    {
        Path = "configuration/system.webServer/modules",
        Name = String.Format("add[@name='{0}']", NameSpace),
        Value = String.Format("<add name='{0}' type='{0}.{1}, {2}' />",
            NameSpace,ClassName,AssemblyName),
        Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
        Owner = String.Format("{0}.{1}", NameSpace, ClassName),
        Sequence = 10
    };

}

and now we can call our module however many times we need from the feature activated event

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    var ErrorSwap_WebConfigMod = GetModule(typeof(ErrorSwapModule));
    SPWebService ser = SPWebService.ContentService;

    if (!ser.WebConfigModifications.Contains(ErrorSwap_WebConfigMod))
    {
        ser.WebConfigModifications.Add(ErrorSwap_WebConfigMod);
        ser.Update();
        ser.ApplyWebConfigModifications();
    }

}

but remember in SharePoint whatever we do we must also undo or pay the price of at least a week of our time.

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    SPWebService ser = SPWebService.ContentService;
    var ErrorSwap_WebConfigMod = GetModule(typeof(ErrorSwapModule));

    if (ser.WebConfigModifications.Contains(ErrorSwap_WebConfigMod))
    {
        ser.WebConfigModifications.Remove(ErrorSwap_WebConfigMod);
        ser.Update();
        ser.ApplyWebConfigModifications();
    }

}

now that we've made our web config modifications we have one more step to do, we have to actually deploy our Http Handler into the GAC, to do this open up your package manifest.


double click on your package and now hit ad assembly from project output

next from the source project drop down select your swap library, where your http module resides

after that make sure to add your namespace as a safe control

with your namespace filled in click on the location text box and this will enable the ok button, hit the ok button and deploy your project:
your deployment should

  • put your http handler into the gac
  • deploy your custom error pages to the pages library of your root site
  • add the appropriate web config entry


Thursday, 3 April 2014

Get MetaData of pages

this poweshell script will iterate over all the pages in a pages library find ones that have a department field with a value that contains the word Information, then write them to the screen. because i use write-output instead of write-host you can pipe them to a text file like I show in the second example
<#
.Synopsis
   List IT Policy & Procedure MetaData
.DESCRIPTION
   Script used to list Information and Technology Policies and procedures
.EXAMPLE
   Get-spmPolicyProcedureMetaData -WebAddress http://YourSite/PolicyProcedure
.EXAMPLE
   Get-spmPolicyProcedureMetaData -WebAddress http://YourSite/PolicyProcedure | Out-File c:\steve1.txt
#>
function Get-spmPolicyProcedureMetaData
{
    Param
    (
        # Web address contianing policies and procedures 'http://yoursite/PolicyProcedure'
            [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$False,
                   Position=0)]
        [string]
        $WebAddress
    )
      Begin
      {
            $web = get-spweb $WebAddress
            $pageLib = $web.Lists["Pages"]
      }
      Process
      {
            $PageLib.Items | %{
                  $page = $_
                  if($page["Department"] -like '*Information*'){
                        $pageName = "Page Name = " + $page.Name
                        Write-Output $pageName
                       
                        $dept = "Department = " + $page["Department"].trimstart("23;#")
                        Write-OutPut $dept
                       
                        $eff = "EFF Date = " + $page["Published Effective Date"].trimstart("string;#")
                        Write-Output $eff
                       
                        $cr = "Council Resolution Number = " + $page["Council Resolution Number"]
                        Write-Output $cr
                       
                        $desc = "Description = " + $page["Comments"]
                        Write-Output $desc
                        Write-Output ""
                  }
            }    
      }
      End
      {
            $web.Dispose()
      }

}

you can run this by entering

PS C:\dev> . . /filename.ps1
PS C:\dev> Get-spmPolicyProcedure -webaddress http://yoursite/yourweb