The Black Knight Sings

Songs about SharePoint and other adventures by Per Jakobsen


Date: # Monday, July 20, 2009

Title: List Quota and Locklevel of all Site Collections in Farm


Here is another small utility requested in one of the SharePoint newsgroups

It creates a tab delimited list of all the Site Collections in the current farm

using System;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Administration;

 

namespace ListQuotas

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("Site Coll name\tSite Coll Url\tIs ReadLocked\tIs WriteLocked\tQuota in bytes");

            SPSecurity.RunWithElevatedPrivileges(delegate

            {

                foreach (SPWebApplication webapp in SPWebService.ContentService.WebApplications)

                {

                    foreach (SPSite sitecoll in webapp.Sites)

                    {

                        Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}",

                            sitecoll.RootWeb.Title,

                            sitecoll.Url,

                            sitecoll.ReadLocked,

                            sitecoll.WriteLocked,

                            sitecoll.Quota.StorageMaximumLevel);

                        sitecoll.RootWeb.Dispose();

                        sitecoll.Dispose();

                    }

                }

            });

        }

    }

}


The executable can be found here and the source code here



Monday, July 20, 2009 6:34:20 PM (Romance Standard Time, UTC+01:00)  #    Comments [2]

Title: List all SharePoint groups a user belongs to


This is just a small Utility requested on one of the SharePoint Newsgroups

It uses the UserGroup web service to list all the SharePoint Groups a user belongs to in a specified Site Collection

It's a small console application which requires two parameters:
···The first parameter is the Url of the site collection including protocol (example: http://localhost)
···The second parameter is the full username including Domain (example: MOSSWORK\user)

using System;

using System.IO;

using System.Xml;

using ListUsersGroups.ug;

 

namespace ListUsersGroups

{

    class Program

    {

        static int Main(string[] args)

        {

            if (args.Length < 2)

                return help();

            try

            {

                UserGroup usergroup = new UserGroup();

                usergroup.UseDefaultCredentials = true;

                usergroup.Url = args[0]+"/_vti_bin/usergroup.asmx";

                XmlNode groupXml =usergroup.GetGroupCollectionFromUser(args[1]);

                XmlDocument doc = new XmlDocument();

                doc.Load(new XmlNodeReader(groupXml));

                XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);

                nsmgr.AddNamespace("sp", "http://schemas.microsoft.com/sharepoint/soap/directory/");

                foreach (XmlNode group in doc.SelectNodes("//sp:Groups/sp:Group", nsmgr))

                    Console.WriteLine(group.Attributes["Name"].Value);

 

            }

            catch (Exception ex)

            {

                Console.WriteLine("{0}\r\n{1}", ex.Message, ex.StackTrace);

            }

            return 0;

        }

 

        private static int help()

        {

            Console.WriteLine(@"{0} ", Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs()[0]));

            Console.WriteLine(@"  Site-url: Url of site collection including protocol (example http://localhost)");

            Console.WriteLine(@"  username: Full username including domain (example MOSSWORK\user)");

            return -1;

        }

    }

}


This execuable can be found here and the source code here



Monday, July 20, 2009 2:01:05 PM (Romance Standard Time, UTC+01:00)  #    Comments [3]

Date: # Sunday, April 26, 2009

Title: Giving end users a list of all the SharePoint sites they have access to


When You’re running a medium sized SharePoint Farm you may often get requests from end users for a list of all the SharePoint sites they have access to.

I’ve implemented a small feature which provides this information as an application page, which you can access through a new menu item on the Welcome menu.

The implementation of this is quiet simple:

Add a menuitem to the Welcome menu pointing to our own application page:

    <CustomAction Id="6346660F-707E-40d2-A7CF-AB6BAC7A98FD"

                  Location="Microsoft.SharePoint.StandardMenu"

                  GroupId="PersonalActions"

                  Sequence="400"

                  Title="Show All Sites"

                  Description="Shows all sites which you have access to">

        <UrlAction Url="_layouts/ShowAllSites/ShowAllSites.aspx" />

    </CustomAction>

 

The application page just contains a ASP.NET TreeView control and the following code:

Get Login of current user:

            login = SPContext.Current.Web.CurrentUser.LoginName;

 

Loop through all SiteCollections inside RunWithElevatedPrivileges:

            SPSecurity.RunWithElevatedPrivileges(delegate()

            {

                SPWebService cs = SPWebService.ContentService;

                foreach (SPWebApplication wa in cs.WebApplications)

                {

                    foreach (SPSite site in wa.Sites)

                    {

                        using (SPWeb web = site.RootWeb)

                        {

                            AddWebToTreeIfAccess(web, tvSites.Nodes);

                        }

                        site.Dispose();

                    }

                }

            });

 

Skip Shared Services Administration and My Sites:

            if (web.WebTemplate == "OSRV"

             || web.WebTemplate == "SPSMSITEHOST"

             || web.WebTemplate == "SPSPERS")

                return;

 

Process all child sites:

            TreeNode tn = new TreeNode();

            foreach (SPWeb childweb in web.Webs)

            {

                AddWebToTreeIfAccess(childweb, tn.ChildNodes);

                childweb.Dispose();

            }

 

Check if user can view pages on this site:

            bool userHasAccess = web.DoesUserHavePermissions(login, SPBasePermissions.ViewPages);

 

Add site to treeview if user can access it or any of it's decendents:

            if (userHasAccess

             || tn.ChildNodes.Count > 0)

            {

                if (userHasAccess)

                {

                    tn.Text = web.Title;

                    tn.NavigateUrl = web.Url;

                }

                else

                {

                    tn.Text = "(" + web.Title + ")";

                    tn.SelectAction = TreeNodeSelectAction.Expand;

                }

                treeNodeCollection.Add(tn);

            }

 

A prebuilt solution package can be found HERE and the full source code HERE



Sunday, April 26, 2009 3:59:37 PM (Romance Standard Time, UTC+01:00)  #    Comments [2]

Date: # Friday, December 19, 2008

Title: Custom pagers for SPGridView


I don't think that the built in pagers for SPGridView are the most useful and informative, so for one of my current projects I decided to implement two custom pagers

The first was XofYPager which shows the page numbers like this:


The second was SmartPager which shows the page numbers like this:


The use of these pagers are very simple just include the classes below and then before you DataBind() your SPGridView set the PagerTemplate property to an instance of one of these classes.



Friday, December 19, 2008 10:16:11 PM (Romance Standard Time, UTC+01:00)  #    Comments [5]

Title: Caching a SharePoint list as a datatable


For one of my current projects I needed to show a SPGridView with data from a small list on the frontpage of the Intranet.

The list wasn't very small so didn't want to read if from the database on every hit of the frontpage, but on the hand it was so big that I couldn't cache it.

Now the big question was how to cache it.
If I used PortalSiteMapProvider then it'll always be refreshed when there was a change, but it would be hard to get the data into the SPGridView for displaying, sorting, paging and filtering.
If I on the other hand justed cached a DataTable, then it'll be easy to use in the SPGridView, but then I'd have to figure out when to invalidate the cache. It should be often enough that users wasn't annoyed with out of date data, but seldom enoughtthat it didn't annoy them due to the performance hit.

So I decided to combine the two and use the PortalSiteMapProvider to figure out when an update was needed and the DataTable for the real caching of the list items.

If you need something similar then here is my class, it doesn't deal with item level permissions, so if you need that you'll have to implement something different:



Friday, December 19, 2008 9:57:40 PM (Romance Standard Time, UTC+01:00)  #    Comments [0]

Date: # Sunday, November 16, 2008

Title: WCMexport version 1.0


When you're branding a site using SharePoint 2007 Web Content Management, you really want to spend your time changing Master Pages and Page Layouts. But you often find that you spend a lot of your time editing your Feature.xml and different Elements.xml files especially if you have a lot of different Page Layouts with web parts for different parts of the site.
WCMexport is a small utility which can help you if you don't like doing all the editing of the xml-files from scratch.
With WCMexport you can use SharePoint Designer or what other tool you want to create the Master Pages, Page Layouts and Style Library Files on a development site.



Sunday, November 16, 2008 1:52:46 PM (Romance Standard Time, UTC+01:00)  #    Comments [0]

Date: # Thursday, August 21, 2008

Title: Turn Default Upload Overwrite Off in WSS 3.0


By default the Overwrite checkbox in Upload.aspx is checked in WSS 3.0 and MOSS 2007, but sometimes you may want to change this default.

MOSS junkie has a post on how to change that default.
But his solution has the downside of having to modify one of the standard application pages which is unsupported and may be overwritten by a upgrade

So how do we change the default without modifying upload.aspx?

Once again the <delegate> controls in the default masterpage can help us.
This time it's the AdditionalPageHead delegate we can use. This delegate allows you to add any number of usercontrols to the page header of every page in sites where your feature is activated.
So we add a feature with the following elements.xml file:



Thursday, August 21, 2008 11:52:41 AM (Romance Standard Time, UTC+01:00)  #    Comments [7]

Date: # Friday, August 15, 2008

Title: Remove the Search Box from SharePoint Sites


Sometimes you don't want your users to be able to search on your SharePoint site.
Part one of this is then to remove your site from the content source in SSP, but you probably also want to remove the Search box from every page in your site.

Fortunately SharePoint makes this very easy

The different version of SharePoint has different requirements for the Search box and the way these different serach boxes are implemented are using the Delegate feature of WSS
Delegates are controls in a masterpage/page whose content is determined by which Features are activated.
Each Delegate has an ID (SmallSearchInutBox for the Search box) and each Feature can vote for it's content by specifying an Control element.
For Delegates where AllowMultipleControls are false (like SmallSearchInputBox) the Control with the lowest sequence wins (25 is the lowest sequence used by OOB Feature)
So to disable the Search box all you have to do is implement a Feature which has a Control element with sequence below 25 pointing to an empty UserControls.
If you don't want to develop this yourselves you can just download my implementation which makes a Feature with WebApplication scope.
The source files are here and a precooked solution file is here



Friday, August 15, 2008 8:38:30 AM (Romance Standard Time, UTC+01:00)  #    Comments [1]

Date: # Wednesday, July 16, 2008

Title: Remove Login box when anonymous users download office document from SharePoint Site


When developing Extranet/Internet site in SharePoint you often want to allow anonymous access and this works fairly well.
But there is one are where the out of the box experience fails regarding anonymous access and that is when you allow the users to download Microsoft Office documents. In that case IE/Office pops up a couple of Login dialogs, if the user cancels out of these the document opens as expected, but you really don't want the user to have to cancel a couple of dialogs to open your documents

The problem is that office tries to be intelligent and issues a Microsoft Office Protocol Discovery request to see how much the user is allowed to do, but SharePoint responds with access denied until the users logs in.

The solution I've found is to implement a HttpModule which rejects the Microsoft Office Protocol Discovery request if the user isn't logged in and this gets rid of the Login boxes
The essential code is fairly simple:



Wednesday, July 16, 2008 5:33:49 PM (Romance Standard Time, UTC+01:00)  #    Comments [32]

Date: # Monday, July 14, 2008

Title: FeatureReceiver To Cleanup WebPart Files


Many web-part developers are surprised to see that their web-parts are listed as available even though they have deactivated the feature which contains them

And the users are equally surprised when they then try to add the web-part only to see it fail because the code (and safe-control entry) has been removed.

The problem comes from the fact that deactivating a Feature doesn't remove files provisioned using Module and File entries



Monday, July 14, 2008 6:02:34 PM (Romance Standard Time, UTC+01:00)  #    Comments [5]

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way. And all information or programs are without warranty. Use at your own risk


© Copyright 2012 Send mail to the author(s) Per Jakobsen Feed your aggregator (RSS 2.0)