Monthly Archives: October 2012

Programmatically getting the Site Collection Name

Just was sorting out a quick console app that audited all the details of a MOSS 2007 farm, all Web Applications, down through Site Collections, Sites, Webs and down to list level.  When presenting this to the end user, I wanted to use a cascading drop down, where the user selected the Web Application and then had a nice list of Site Collections to choose from.

The problem I hit was how on earth to get the Site Collection Title and Description.  These are displayed in Central Administration | Application Management | Site Collection List but are not included in the SPSiteCollection object, nor the WebApplication object (which holds stuff like the Content Database which is included on that page).

Answer: the Title and Description that appears on the Site Collection List page is the Title and Description of the root Site in the Site Collection.

Try it!  Check the Title and Description in Central Admin, then change them on the root Site using Site Settings | Title, Description and Icon.

Took me a while to find that one…

Advertisements

Get all SharePoint 2010 database names

I’ve recently been doing what I’ve called a “lift and shift”.  This is moving all the SharePoint databases from one SQL Server to another.  The process is comparatively simple in itself, but the sheer number of databases involved makes it a process where you have to keep an eye on exactly where you are up to.

Anyway, I needed a way to get all the databases I was going to have to move.  Thankfully I found this little Powershell line.

Get-SPDatabase | Sort-Object disksizerequired -desc | Format-Table Name

Note that this returns every database, from Content DBs to the databases assigned to Service Applications.  The link has more info about the various options as well.

JSGrid and Hyperlinks

Now, I might be wrong in this but there is a rather significant flaw in JSGrid.  This is the inbuilt Javascript based Grid for SharePoint 2010.  It is actually very handy to use, if not particularly SharePoint-y.  (For example, when connecting to a SharePoint list, you have to copy the data of out a SPListItemCollection into a DataTable, which doesn’t seem great to me.)

Anyway, if you use a Hyperlink in the JSGrid, it will always open in a new page.  This is massively annoying, not to mention counter-intuitive.  So what is the solution?

There isn’t one.

Basically, the Hyperlink class has two properties, Display and Address.  I’ve dug into the workings of JSGrid and there is a JSGrid.js file in <%14HIVE%>/TEMPLATE/LAYOUTS.  Having had a poke around, I found what I think is the problem.

if(d.bHyperlink)return function(){ULSEWi:;window.open(d.GetAddress(a))}

This looks like – and I stress I may be completely wrong – the bit of code that fires the link, and it is hard-coded to window.open which will always open that new window.

I’ve played around with jQuery and not managed to actually stop the event from firing properly on a regular basis.  I’ve tried stuff like event.preventDefault(), .unbind() and so on.  The closest I’ve got is to .unbind() on the first click of a hyperlink, but it insists on working second time around, so I think there is a refresh going on somewhere.

I’ll leave this one for now to more capable jQuery people, but if you are investigating this further, remember that the HyperLink class sticks the target URL in the click event, not the href of the <a> tag, so simply removing the click event won’t work.

A colleague has suggested a new class inheriting from Hyperlink, which I’ll have a look at.

Seems a lot of work for a simple thing, but that is SharePoint for you.

Individual MySite Quotas

Had a request from a user who had run out of space for storing documents in their MySite.  One of the fun side effects of this in MOSS 2007 is that you can’t use the GUI to change the quota allocated – it throws the standard SharePoint error.

Because the MySite is a Site Collection, you can change the quota on an individual basis.  I knocked together a short console app in Visual Studio 2008.

Build and run it from the Command Line using the syntax:

SiteQuotaChange.exe -url %URL% -quotaname %QUOTANAME%

Notes:
1. Because %QUOTANAME may be something like “Personal Site”, you need to put the quota name in ” marks if it contains a space.
2. If you get %QUOTANAME% wrong, MOSS 2007 will default back to “Individual Quota” with no space allocated.

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace SiteQuotaChange
{
class Program
{
private static string url = String.Empty;
private static string quotaName = String.Empty;

static void Main(string[] args)
{
try {
GetArgs(args);
if (url == String.Empty || quotaName == String.Empty || args.Length > 4)
{
Console.WriteLine("Usage: sitequotachange -url %URL% -quotaname %QUOTANAME%");
}
else
{
using (SPSite site = new SPSite(url))
{
SPWeb web = site.OpenWeb();
Console.WriteLine("Before: " + site.Quota.StorageMaximumLevel);
web.AllowUnsafeUpdates = true;
web.Update();
site.Quota = SPWebService.ContentService.QuotaTemplates[quotaName];
web.Update();
Console.WriteLine("After: " + site.Quota.StorageMaximumLevel);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception thrown: {0}, {1}", ex.GetType(), ex.Message);
}
Console.WriteLine();
Console.WriteLine("Finished, press any key");
Console.ReadKey();
}

private static void GetArgs(string[] args)
{
for (int i = 0; i < args.Length; i++)
{
switch (args[i])
{
case "-url":
url = args[i+1];
break;
case "-quotaname":
quotaName = args[i + 1].Replace("\"", "");
break;
}
}
}
}
}

Programmatically determining whether a List Item is a Record

Was trying to find this one, and the path / references aren’t all that clear.  So…

  1. Add a reference to Microsoft.Office.Policy
  2. In the Usings add using Microsoft.Office.RecordsManagement.RecordsRepository
  3. Get the ListItem or File
  4. Records.IsRecord(item) returns true or false if it is a record

The key bit is figuring out RecordsManagement.RecordsRepository is part of Microsoft.Office.Policy.

MSDN

Using [Me] in CAML Queries

Sometimes you need to put a custom View into a List Definition.  That View could be based on the current user (for example “Show all List Items assigned to me”).  The problem is that the usual GUI method uses [Me] to filter the items appropriately, which breaks CAML.

The trick is twofold.  First, the Value Type is “User” and second, the magic value is <UserID />.  Therefore your code is.

<Query>
  <Where>
    <FieldRef="AssignedTo" />
      <Value Type="User"><UserID /></Value>
  </Where>
</Query>

Hat Tip: Macedonian SharePoint Group (which incidentally also demonstrates how to use [Today] in CAML.  Also MSDN.

Hello world!

This is a blog about SharePoint.  It basically serves as a repository for bits of code that I use every so often keep forgetting whenever I move to another contract.  I don’t claim to be the author of all of the code here, but where I can I’ve credited people appropriately.

The idea is that I make a quick note if I’ve used a handy technique or site and the search engines pick up some of these things and help spread the knowledge around the general SharePoint community.