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