Albert Jan Schot
 

UserProfile and ChoiceLists, reading and saving

19

Feb

Last few days I have been struggling with a piece of code that should update a UserProfile. The idea was to make a nice feature that allows a WebPart to display a link that would open a screen showing the user some options to change UserProfile values. Those change options should be rendered based on three pieces of information; a Site Column containing possible values, a ControlType and the UserProfileProperty to save it to.

The first few versions worked pretty well since I only had to save strings or checkboxes, however further along the way I decided that ChoiceLists from the UserProfile should be supported, and there it all went wrong, even with Google it took me quite some time figuring out how to do it, so here a few tips: 

Saving a single value to a UserProfileProperty even when its a ChoiceList is pretty easy:

    userprofile[item.Key].Value = item.Value;

Saving multiple values to a userProfileProperty took me some more time, but finally even that appeared to be possible:

   foreach (string choice in choiceList)
   {
      userprofile[item.Key].Add((Object)choice);
   }

Minor detail on that part appeared that the .add option does as it says, it only adds items; ergo if you would already have selected option1 in your profile, and try to update it with an option2, it would keep the option1 saved, and adds the option2.

So before you are actually try to save values that way do a:

   userprofile.clear();

   userprofile.update();

Now that’s for ‘saving’ values to your UserProfile, reading them is another story, reading a ‘normal’ string can be done with a simple:

   userprofile["PropertyToRead"].Value;

Reading a ChoiceList should be done like:

   if (userprofile["PropertyToRead"] is UserProfileValueCollection)
            clValue = userprofile["PropertyToRead"] as UserProfileValueCollection;

Whenever you done that you can simple loop trough the UserProfileValueCollection for reading the selected items.

Albert-Jan Schot schreef

Comments (0)

Albert-Jan Schot

Exporting Site Columns

11

Dec

If you ever want to export site columns from an existing site collection, you might find yourself in a bit of a loop, since its quite hard to get a nice ‘export’ of all those columns if your no developer. So when I found out Gary had done that before I thought it was worth a blog.

Just some plain information about ‘exporting’ site columns from an existing site collection:

http://stsadm.blogspot.com/2008/02/export-site-columns.html 

The actual download can be found here:

http://stsadm.blogspot.com/2009/02/downloads.html

I did blog before about them extensions and the use of them in case of Ghosting.

Albert-Jan Schot schreef

Comments (0)

Albert-Jan Schot

Adding a ItemTemplate into a repeater the new way

21

Aug

A colleague recently found a very easy way to add a ItemTemplate into your repeater without having to create the ‘classic’ myTemplate : ITemplate class. So a nice example of how less can be more:

1: rptPager.ItemTemplate = new CompiledTemplateBuilder( new BuildTemplateMethod(

BuildItemTemplate ) );

   2:  
   3:  
   4:         void BuildItemTemplate( Control container ) {
   5:             LinkButton lbPage = new LinkButton() { ID = "lbPage" };
   6:             container.Controls.Add( lbPage );
   7:         }

Albert-Jan Schot schreef

Comments (0)

Albert-Jan Schot

SPList ItemCount on a > 100.000 items list

20

Apr

Having a list containing more than 100.000 items provided us with some challenges, apparently it is not possible to retrieve the ItemCount based on certain criteria using the object model.

The problem is that we actually had to retrieve those items.

So a colleague came up with a rather dirty but very useful  fix for that.

It’s a small piece of code that makes a View based on a desired query, and sort this on the Author (all the items are created by the same account). By setting the Collapse property on true SharePoint renders a HTML Header containing the total amount of items. By actually reading that piece of HTML he found a quick&dirty way to retrieve those totals.

Below you can find the code, and we were wondering if any of you out there would know another way without using the Search or talking directly to the Content Database.

/// <summary>

/// This function will return the count of the items found by the query, using the RenderAsHTML() method of the SPView object

/// IMPORTANT: It asumes that all Items are created using the same account (Author), if this is not the case,

/// please find or provide an other property that is equal for all ListItems

/// NOTE: This function has no error-handling inside.

/// </summary>

/// <param name="list">SPList object that holds the items to count</param>

/// <param name="query">CAML query-string</param>

/// <returns>-1 on error, otherwise number of items found</returns>

private static int GetItemCount(SPList list, string query)

{

    return GetItemCount(list, query, "Author");

}

private static int GetItemCount(SPList list, string query, string GroupByProperty)

    {

        //Since files are added by the system, author will be the same for all

        //adding GroupBy and setting the Collapse to true, the view will be rendered collapsed and show only the itemcount

        query = String.Format("<GroupBy Collapse=\"TRUE\" GroupLimit=\"1\"><FieldRef Name=\"{0}\" /></GroupBy>{1}",GroupByProperty, query);

 

        //create temp view

        list.ParentWeb.AllowUnsafeUpdates = true;

        string TempViewName = "TempViewForItemCount";

        SPView newview = list.Views.Add(TempViewName, new StringCollection() {"Soep1", "Author" }, query, 10000000, false, false);

        newview.Update();

        list.Update();

        //RenderAsHtml creates a small piece of HTML that contains the itemcount

        string html = list.Views[newview.ID].RenderAsHtml();

 

        //remove the temp view

        list.Views.Delete(newview.ID);

 

        list.ParentWeb.AllowUnsafeUpdates = false;

 

        //init a counter

        int count = 0;

 

        //grab the count which is in brackets right after the '&#8206;'-character

        if (html.IndexOf("&#8206;(") <= 0)

            return -1;

        html = html.Substring(html.IndexOf("&#8206;(") + 8, html.IndexOf(")", html.IndexOf("&#8206;(")) - (html.IndexOf("&#8206;(") + 8));

       

        //try to parse the count

        if (!int.TryParse(html, out count))

            return -1;

        return count;

    }

 

 

//testfunction

private void Test()

{

    using (SPWeb myweb = new SPSite("http://somedomain.com").RootWeb)

    {

        SPList SomeList = myweb.Lists["SomeList"];

        Console.WriteLine("End loading web at: " + DateTime.Now.ToLongTimeString());

        string q = "<Where><Eq><FieldRef Name=\"SomeField\" /><Value Type=\"Text\">SomeValue</Value></Eq></Where>";

        int count = GetItemCount(MenuVanDeDag, q);

    }

}

 

Albert-Jan Schot schreef

Comments (0)

Albert-Jan Schot
Page 1 of 1 in the SharePointObjectModel category