Albert Jan Schot
 

Getting ParentFolder from a custom list

23

Feb

Getting items from a list is something most developers do a lot, and one of the nice functions you can use is the SPListItem.File.ParentFolder since it returns (what else couldt it do), the parentfolder. Especialy when you’re creating something with navigation like breadcrumbs or tabs, that function will provide you with enough information to display the desired info.

However this seems to have a little limitation, it only works on standard libraries that actually contains ‘items’ which means that if you are trying to use it on a custom list it won’t work. And most developers use custom lists / content types. I hit the same problem, but luckily someone posted a nice solution to this on MSDN (credits go to Donzola).

The following code will allow you to get the parent folder of a list item:

/// <summary> 

/// Returns the parent folder of an item in a list. 

/// </summary> 

/// <param name="itemToFind">Item whose parent folder you are looking for.</param> 

/// <param name="folder">Folder object to look in.  Pass null to start from the root.</param> 

/// <returns>SPFolder object of parent folder or null if item isn't in a folder or if the item isn't found.</returns> 

static SPFolder GetParentFolder(SPListItem itemToFind, SPFolder folder)

{

      SPQuery query = new SPQuery();

      query.Folder = folder;

      SPListItemCollection items = itemToFind.ParentList.GetItems(query);

     

      foreach (SPListItem item in items)

      {

            if (item.ID == itemToFind.ID)

            {

                  return (folder);

            }

            if (item.Folder != null)

            {

                  SPFolder resultFolder = GetParentFolder(itemToFind, item.Folder);

                  if (resultFolder != null)

                  {

                        return (resultFolder);

                  }

            }

      }

 

      return (null);

}

So adding that function allows you to get a parentfolder with:

SPFolder parentFolder = GetParentFolder(item, null);

Final note; make sure that you check if the parentFolder != null before you use it.

Albert-Jan Schot schreef

Comments (0)

Albert-Jan Schot

Naming office files in a Library

18

Feb

So everyone running MOSS works with libraries, and working with office files stored on libraries. Most of the times it works as a bliss, but there a few notes. Within the naming of folders in your library NEVER use the following characters, since while your SharePoint works nicely with them, your Word or PowerPoint will actually fail trying to open the file, giving  you an error that it cannot be read.

  • $
  • &
  • +
  • ,
  • /
  • :
  • ;
  • =
  • ?
  •  @

Just refrain from using these characters either in your filename, folder name or anything else that goes in the url. And make sure all your users know about this.

Albert-Jan Schot schreef

Comments (0)

Albert-Jan Schot

Sorting a list on a full month name

12

Feb

Sorting a list on Month by using a calculated field. Ever wanted to sort your view of a list based on Months, still wanting to display January, February. The result of sorting on that column would actually giving you an A, B, C result rather then returning them in chronological order.

Getting that done can be quite easily obtained by using calculated fields, since they allow you to 'as the name says' calculate values based on other values already stored on your columns, and that what we want, though before we start some basic understanding of the calculated fields might come in handy.

A calculated field is always a 'new' value based on existing ones, and can be of the type: string, number, value, date/time or yes/no. You can find some more information about all of the functions on http://office.microsoft.com/en-us/sharepointtechnology/HA011609471033.aspx (make sure you check that one out, since in this post I will only cover a few basics.

Let’s say we have a first name, last name and we want to concatenate a Full Name

=[column1]&" "&[column2] // column1, column2

Saying that instead of writing the full first name you could also 'split' it, and only use the first character

=LEFT([column1]; 1)&". "&[column2] // c. column2

Enough about the basics, you can add a calculated column and use the =TEXT([DateColum], "mm") to get the month value into a text field (01, 02, 03 instead of the Formatted dates). Next thing, just sort your view on that column, and hide it, and show your nicely formatted date (something like January, February) and as you will see its nicely sorted.

 

Albert-Jan Schot schreef

Comments (0)

Albert-Jan Schot

Debugging Masterpages

06

Feb

Have you ever spent time trying to debug a masterpage? Like every SharePoint developer I spent my days on the SharePoint platform, and since ‘everything’ you see in SharePoint is based on masterpages, it would be nice if they do what they should. However a lot of customers require customized masterpages, and a lot of these masterpages work just fine, and if they don’t you can easily debug them, or not.

Last week we had a nice issue; in IE6 (yea IE6) our masterpage didn’t show the navigation controls in the edit navigation page. Seems out the masterpage been missing a three divs:  

    <div id="zz1_CurrentNav_Data" style="display:none;"></div>

    <div id="zz1_TopNavigationMenu_Data" style="display:none;"></div>

    <div id="zz2_CurrentNav_Data" style="display:none;"></div>

 

Adding those divs (even with a display:none) will fix a lot of these errors. And there is a nice way to find out what other divs are required by SharePoint to show all data.  Since often you can see the error in the lower left of the screen (often something like: Scripts loading …), the underlying issue can be found in the JavaScript.  Just browse to the Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033 (make sure to change the 1033 to your regional code, so in my case it would be 1043 and make a copy of the BFORM.js, then edit the original one.  On rule 12418 you can find the var named L_PleaseWaitForScripts_Text this var is used to display errors, now start at the top of your document and search for it. You will see that its only used in the SPOnError_HandleErrors function, where you can find:

                     {
                                window.status=L_PleaseWaitForScripts_Text;
                     }

Just add a simple alert above the window.status that displays the msg passed to your function.

                     {
                                alert(msg);
                                window.status=L_PleaseWaitForScripts_Text;
                     }

 

Next time you open the page you want you will get a nice popup containing your error. Nice feature to debug your pages. (Though its only executed when the window.status is not complete)

 

Albert-Jan Schot schreef

Comments (0)

Albert-Jan Schot

Missing links in Central Admin

03

Feb

Running your Central Admin on a win2k8 machine adds a nice feature: It hides certain links when you don’t run your IE as Administrator, in other words, links like create or extend web application’ of ‘services on server’, are only showed to a Local Admin. Quite frustrating experience if you’re trying to setup a new web app not knowing about that.

Albert-Jan Schot schreef

Comments (0)

Albert-Jan Schot