HiddenNetwork.com Banner
Showing posts with label SharePoint 2007. Show all posts
Showing posts with label SharePoint 2007. Show all posts

Thursday, January 10, 2008

Getting user profile values that support multiple value

I noticed today that the MSDN article "User Profiles Object Model Overview" has a wrong code sample for the "correct way of retrieving a user profile property value". The code sample there will not work, because the object model in sharepoind doesnt have the methods and objects the sample code is using.
Therefore - here is my correct way:

if (Profile[this.UserProfilePropertyName].Count == 1)
    return Profile[this.UserProfilePropertyName].Value.ToString();
else
{
    StringBuilder ret = new StringBuilder("");
    UserProfileValueCollection values = Profile[this.UserProfilePropertyName];
    System.Collections.IEnumerator allValues = values.GetEnumerator();
    while(allValues.MoveNext())
    {
        ret.Append(allValues.Current.ToString());
        ret.Append(";");
    }
    return ret.ToString();
}

Friday, September 28, 2007

Wrokaround - "Error" in navigation when creating a publishing site from code

Since I started working with sharepoint 2007 I was puzzled by this bug, and recently I asked more people how said they have that too. The problem happens when you use the API to create a site out of a template that has the publishing feature activated in it. The site is created ok, but the first user to open the site will see a very odd tab and quick-launch:

So far, I have no explanation, but I have a workaround. Make a call to the site in the code. I know - this adds process time and resources, but it beats having your users ask you about the "error tab".
In your code, add the following function, and call it just after the site creation code:

public static void OpenUrl(string url)
{
    try
    {
        HttpWebRequest request = (HttpWebRequest)
        WebRequest.Create(url);
        request.UseDefaultCredentials = true;
        HttpWebResponse response = (HttpWebResponse)
        request.GetResponse();
    }
    catch(Exception ex) {
    //do something with the error
    }
}
Here is an example how to use this function:
public static void CreateSite(SPWeb parentWeb)
{
    try
    {
        //get the template for the site using the GetWebTemplate function I wrote
        SPWebTemplate template = 
                SharePointFunctions.GetWebTemplate(parentWeb,
                        this.SiteTemplateName, 
                        ConfigVariables.SitesLCID);
        //create the site using the class properties
        newWeb = parentWeb.Webs.Add(this.ShortName, 
                this.SiteTitle, 
                this.SiteDescription, 
                ConfigVariables.SitesLCID, 
                template, 
                false, 
                false);
        OpenSiteHomePage(newWeb.Url);   
    }
    catch(Exception ex) {
    //do something with the error
    }
}

Tuesday, June 26, 2007

Update to ItemAdding fiasco

This is to let you know that I updated my posts about the event handlers and ItemAdding, after I did some more research and found the correct way to get and set item's properties during ItemAdding. I swear this method did not work during Beta2 when I posted my old posts, and I swear that contacts in Microsoft did tell me it was by design and not possible (I have the emails to show that).
However, it turned out that it is possible, and easy, and I posted an update and a code sample in the old posts:
Bad news - synchronous list events bug (or missing feature)
Synchronous Add List event (ItemAdding) will not give access to item properties

Thursday, June 21, 2007

Great MSDN developer article - Development Tools and Techniques for Working with Code

There is a new article in MSDN which enraptured me. It is a magnificent summary of what development in sharepoint is all about, with example on how to do sample tasks.
It is in two parts, and is from my fellow MVP Patrick Tisseghem, (from U2U, Patrick is one of the authors of my favorite sharepoint developer utility - the CAML Query Builder) Development Tools and Techniques for Working with Code in Windows SharePoint Services 3.0 (Part 1 of 2)
Development Tools and Techniques for Working with Code in Windows SharePoint Services 3.0 (Part 2 of 2)

Wednesday, June 20, 2007

Adding custom fields to the Enhanced Content Quey Web Part (Beta 1.2)

I just got around to adding another important feature to the Enhanced Content Query Web Part and I uploaded it to codeplex.
I am sure you read my articles, or articles by other MVPS like Heather about showing custom columns in the content query webpart. This requires (as Heather's article shows) that you export the web part to a file, manually modify the file to add the fields you want, and import it back.
This annoyed me enough to add a property to my ECQWP that will allow you to add those fields without having to export-import.

In this version (1.2) you have two new properties in the propeties pane - Data Fields and Common View Fields. I have yet to find out how Data Fields are used, so you don't need to use that property - it is enough that you set the Common View Fields.
I will refer you to Heather's article to find out the format that the value of this property should be written in - how to get the field's internal name, and how to get the field's type. Just a quick reminder though - the value in that field is a pipe seperated value of fields, where each field is a coma seperated pair of field (internal) name and field type. For example, a text field with internal name "Comments" will be added like so:
Comments,Text
A choice field with internal name "Client_x0020_Name" will be added like so:
Client_x0020_Name,Choice
and if we want to add both of the above fields, it will look like so:
Comments,Text|Client_x0020_Name,Choice


As always, get the web part, installation instructions and source code from codeplex. Just be sure to download the most recent version.

Wednesday, June 13, 2007

Using Javascript to Manipulate a List Form Field

This is something I have been doing for a while (see my nested tasks project - it has been using this trick), and now Rob Howard from Microsoft published a tip on how to use javascript to manipulate a list form field.
The example of use I would like to give is my nested tasks project- we need to set a default value when a user creates a sub task. We have a web part sitting in the "newform.aspx" page, and checks the querystring the page was opened with. If the page was opened with a querystring of "parentID=", then it loads the item that is the parent, gets the values for start date and end date, and then uses javascript to set the "Parent Task", "Start Date" and "End Date" fields.
My javascript also hides the "Parent Task" field control from the user, so he will not edit it manually. This gives us a very user friendly interface.

The same trick can be used to implement some custom validation - for example not allowing the user to enter a start date before the start date of the parent.

Wednesday, June 06, 2007

My favorite tool just got better!

U2U have released a new version of the U2U CAML Query Builder, and this time it can be installed as a feature, and has a web interface instead of the windows form application it was so far. GREAT!
Check out the post from Karine Bosch.
If you don't know what the CAML Query Builder is, I suggest you start by downloading it (either the new release or the old one) and see how easy it is to write CAML queries with it.

Allowing InfoPath forms to redirect to pages in other site collections

In InfoPath 2007, when you publish a form as a web form, you have the option to specify several query string parameters. One of those parameters is the "Source" parameter, which is "The location to which the user will be redirected when the form is closed. The URL must be in the same site collection or an error will be returned."
The error mentioned is "The following location is not accessible, because it is in a different site collection:" and the url you specified.

So it seems that specifying a source from another site collection is impossible. But what if I have a site collection dedicated to forms? lets say "http://forms", to which I link from the intranet home page or another page, and I want that when the user closes the form, he will be redirected back to the page in the intranet (lets say "http://intranet") that we started from?

I had to come up with a solution, and a simple one jumped to mind - a redirect page!
The trick is simple. Instead of specifying a link to "http://intranet" as the source parameter (which will cause an error), specify a link to a special page in the layouts folder, and give that page the query string that links to the page you want to return to.

Confused? here is an example:
Instead of linking to the form with this link:

http://forms/_layouts/FormServer.aspx?XsnLocation=http://forms/myforms/Forms/template.xsn&Source=http://intranet

Link to it like this:
http://forms/_layouts/FormServer.aspx?XsnLocation=http://forms/myforms/Forms/template.xsn&
Source=http://forms/_layouts/SPSTipsFormRedirector.aspx?target=http://intranet

As you can see from the links, the first one will cause an error because we are linking to another site collection, while the second one will work, because we are linking to the current site collection.

So the only thing left for me to show you is how to create the SPSTipsFormRedirector.aspx page that will handle the redirect to the page you specified in the red section in the second link:
Open your layouts folder (C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS) and create a new text file by the name SPSTipsFormRedirector.aspx. Open the file in a text editor such as notepad, and copy the following code:

<%
string target = Page.Request.QueryString["target"];
Page.Response.Redirect(target);
%>
<%
@ Page Language="C#" Inherits="System.Web.UI.Page" %>
<html>
</
html>

Repeate this in every front end server, and you are ready to write links to forms that, when closed, will go back to a page you specify in the link!

Tuesday, June 05, 2007

Enhanced Content Query Web Part goes Beta 1!

Hey Everyone!
This is to let you know that I released beta 1 of the Enhanced Content Query Web Part Project. Make sure you read the installation documentation to the end, because that is where you will find the sample XSLT that will cause the menus to appear.

Anyone want to volunteer to draw me a logo? Contact me!

Monday, June 04, 2007

Coming Soon! Content Query Web Part with Context Menus

Well, I promised you something that will knock your socks off, so hold on!
I have started a new open source project for an Enhanced Content Query Web Part that will have the enhancement (in the first stage) of Context Menus.
(note - the project is not yet published - I need to figure out why my laptop suddenly refuses to connect to codeplex, and I will release the beta)
Content Query Web Part for MOSS with Context Menus

Additional features that will come with this release include the ability to add the default toolbar (new, updload, actions, settings) to the web part when it is connected to a specific library or list (not in aggregation mode)
Content Query Web Part with Toolbar
Some things will not work as you'd expect (if you select a view using the view selector it will redirect you to the view, not change the current web part) but it is a very useful addition to the web part's abilities!

I intend for this project to grow to be a true replacement of the content query web part, and that is why I am calling this project "Enhanced Content Query Web Part".
I hope others who have done work around enhancing the built-in web part like Meron Friedman (Content Query Web Part with marquee) and Steven Van de Craen (extended Content Query Web Part with item limit per group *link seems to be broken?*) will join me as developers on this project, and integrate their code with mine.

I am also looking for advanced developers to help me get my current code stabilized. If you want to help - contact me.

Monday, May 28, 2007

Meanings of variables in the context menu's script

If you are building a web part that displays context menus for list items - just like the context menus that SharePoint builds out of the box, you need to know the meaning of the attributes that you need to add to the table that holds the item. I couldn't find any documentation or referance to it on the internet, so I decided to publish the results of my diggings.
If you look at the source html of the built-in web part,you will see that every item with a context menu looks like this:

<table height="100%" cellspacing="0" class="ms-unselectedtitle" onmouseover="OnItem(this)" id="1"
ctxname="ctx1"
url="/Documents/Sample%20Document.docx"
dref="Documents"
perm="0x7fffffffffffffff"
type=""
ext="docx"
icon="icdocx.gif|Microsoft Office Word|SharePoint.OpenDocuments"
otype="0"
couid=""
sred=""
cout="0"
hcd=""
csrc=""
ms="0"
ctype="Document"
cid="0x0101006121B9A5B6A75F49AD8620D335B47E62"
uis="1024"
surl="">

The question is asked - what do those attribute mean? what is "cout" and what is "otype"? what values are expected there?

After some research, here is my dictionary:

  • ctxname: Name of the context object on the page that holds the context for the list that you are connecting to.
  • url: link to the document (can be either relative or absolute.
  • dref:File Directory Referance - the relative folder path the item\document is in. For example if I have a document library called "documents" with the url "http://portal/documents", the items in the root folder will have "documents" as the dref value, while items in a folder called "test" will have "documents\test".
  • perm: has to do with permissions, but I didn't figure out how it works yet.
  • type: Seems not to be used for items - but to create menu seperators or different kind of menus.
  • ext: The document extension. For example "doc", "docx", "ppt" and so on.
  • icon: This seems to control the "edit" menu item. Here you specify the icon that will be displayed next to the "edit in..." menu item, the text that will appear as the name of the application (edit in Microsoft Word) and the script that will be used to edit the item (I have no idea what possible values can be used here). All of this seperated by the "|" character.
  • otype: Current Item FSObj Type. This seems to have something to do with the check in-check out menus, that will not be displayed if this is 1, but I could not figure out what is the logic here.
  • COUId: Current Item Checked Out User Id. (ID of the user in the web site the item is from)
  • sred: Server File Redirect. Allows you to specify a URL that will be used instead of the file's url when the link is clicked or when a menu option is used. For example, if you put a link to another document, all menu actions will be redirected to that document instead.
  • cout: Current Item Checkedout To Local. If the item is checked out to a local (offline) folder and not to the database. 0 for false, 1 for true.
  • hcd: Controls if a menu item to navigate to the "/_layouts/updatecopies.aspx" will be added. I am guessing this has to do with records management (update copies of the document when it was changed).
  • csrc: Copy source link. If the item is a copy of an item in a records management site, this holds the link to the source, and adds the menu item for going to the source file (Go to source).
  • ms: Current Item Moderation Status. Also has to do with if its checked out or not, and if its draft or not.
  • ctype: The type of the item. I could not find where that is used.
  • cid: Content Type ID. The ID of the content type for the item. also seems to be used for list menus to determine the content type to create when a user clicks on the new item menu.
  • uis: Current Item UI String. Seems to hold the version number, where the major version is a multiplication of 512, and the minor version is the reminder (the script gets the minor version number by doing "%512"). For example, a value of 512 is version 1.0 while 513 is version 1.1 and 1024 is version 2. Does this mean we have a version limit of 511 minor versions in sharepoint? According to the "introduction to versioning", this is only the default and the administrator can change this. This sounds fishy considering the "512" limit is hard coded in the javascript.
  • surl: Source Url. This dictates where the page will go back to after a menu item is clicked.

Wednesday, May 23, 2007

Content Query Web Part xsl for showing the xml

I keep forgetting this, and I think I had to figure it out from scratch at least 4 times allready, so I may as well blog it.
The thing is - last time I figured it out I wanted to blog about it, and then saw that someone beat me to it - Heather Solomon already wrote about it in her blog, and I seem to remember someone else wrote a similar script.
The thing that throws me off every time is realizing that the XML that the content query web part builds for each item has the values in the attributes. So what you need to do is write xslt code to display the attributes.

Heather's article says exactly how to modify the files, so if you need that kind of help, go there now. Below is the code that I am using (slightly modified):

<xsl:template name="ShowXML" match="Row[@Style='ShowXML']" mode="itemstyle">
    <xsl:for-each select="@*">
        </br>
        Name: <xsl:value-of select="name()" /> Value:<xsl:value-of select="." />
    </xsl:for-each>
</xsl:template>

I have just managed to do something really cool with the web part, but I have to refine it a bit before I publish it. I promise this will knock your socks off!

Friday, May 11, 2007

SharePoint Designer Article 2 - Creating an XSLT DataView for a sharepoint list

An XSLT dataview is a great way to modify the look and feel of displayed data in a sharepoint site. The dataview web part is able to display data from either sharepoint lists, or from external data sources.
This article will focus on how to create a simple xslt dataview for a sharepoint list.

First we will create a sharepoint list - Contacts, and fill it with some sample data:


Now, lets create a new page that we will use to create the xslt dataview in:


Open the page in SharePoint Designer (see my previous article if you forgot how - you will need to detach it from the page layout first!).


Now we can add the contacts list to the page using sharepoing designer. You can also do that from the browser before opening the page in sharepoint designer if you prefer:


Let us work in a clean environment - change the properties of the web part so it doesn't display the toolbar. Annoyingly, in sharepoint designer this means you have to open the dropdown menu for the web part (looks like a list icon) and select "change layout":


Select not to show a toolbar.
Now we can transform the web part to an XSLT dataview. Just right-click the web part and choose "convert to xslt data view":


The page should flash and then the web part will appear again, only slightly different. A new menu will appear with a lot more options (filter, sort and group, paging, edit columns, change layout, data view preview, show with sample data, conditional formatting, web part connections, parameters, refresh dataview, dataview properties).
Now you can start playing with it!

In this article I will not go over all the options - I will just conclude by showing you the conditional formatting, and how to deploy the web part to another page in the site:

Click on "Conditional Formatting" in the common data view tasks menu:


The task pane on the right should change to the "Conditional Formatting" pane. Now we can click a row (any will do), and then click the "create" menu, and choose "apply formatting" from the menu:


Because we selected a row, we can apply formatting to that row based on rules we will now define in this screen:


In this example I chose the rule - when first name is "Ishai". Press OK to get to the formatting options:


I set the formatting to show my contact's row with a light blue background, in bold and in uppercase.
Save the file, and view it in the browser:


As you can see - it shows exactly what I wanted it. Now to deploy it to the home page of the site (which we didnt want to detach from the layout page):
In the browser, open the actions menu for the web part and click "export":


Now save the file to your desktop, and go to the homepage of the site. Switch to "edit page mode", click on "add a web part" in one of the zones, and then on the "advanced web part gallery and options" link at the bottom.
Use the dropdown at the top of the pane to switch to web part import pane:


Now, browse to the saved file and click the "upload" button. The web part should appear as an option in the pane. Drag and drop it to the zone you want it in:




And now you can click on "Exit Edit Mode" and see your wonderful new home page, that is still attached to it's layout page!

The next article will show how to do the same, but with external data!

Monday, April 30, 2007

SharePoint Designer Article 1 - how to edit a page?

In SharePoint 2003 (WSSv2) with FrontPage, it was so easy to edit a page. Just open the page you want to edit in Internet Explorer and hit the "Edit in FrontPage" icon at the toolbar.
Well, the good news are that in SharePoint 2007 (WSSv3) the same applies, but only for some of the pages, while other pages will tell you "This page cannot be edited in SharePoint Designer".

This article will explain what is going on, and how to get each type of page to be editable in SharePoint Designer.

Lets start easy- when you create a WSSv3 web site from one of the default template such as team site, blank site etc...you will have no problems using the easy button - edit in SharePoint Designer:

You can also open the SharePoint site or page for editing from within SharePoint Designer using the "Open Site" or "Open.." dialogs within the application:

When you open such a page from such a site directly from Internet Explorer, or from SharePoint Designer, you will see the page in SharePoint Designer, and be able to modify it using the designer mode or the code mode:

So far so good right?
So lets figure out why some of the pages give us the following message when we try to edit them in SharePoint Designer:
"This page cannot be edited in SharePoint Designer. You can edit the content in the browser, or edit the corresponding page layout in SharePoint Designer"
This page cannot be edited in SharePoint Designer. You can edit the content in the browser, or edit the corresponding page layout in SharePoint Designer

Why is that? well, simply because the page you are trying to edit is under the publishing feature. It is a publishing page, and as such, by default, gets it's layout from the layout page and the master page.
The only change you should be doing on such a page is edit it in the browser and add\remove\change web parts in it.

However, there is a way to work around this - detach it from it's layout page. This is similar to the ghosting\unghosting process that we had back in SharePoint 2003 (WSSv2), but with the added benefit that we can allways roll back the change.
It also means that in the first time you will edit that page, you will have to start from SharePoint Designer to do it:
Open the site that contains the page in SharePoint Designer (use "File>Open Site") and browse to the page you want to edit in the folder list.
Right click the file, and choose "Detach from page layout". This will unghost the file - copying it's layout from the layout page into the database, and so allowing you to edit it, just like you used to do in SharePoint 2003:

Unlike sharepoint 2003, you can take it back, and reattach the file:

Best practice:
Although unghosting is no longer a big bad wolf since it can be undone, you still shouldn't be doing it. It means that the page is disconnected from the layout page that defines the layout for all of the pages in the site. If you will want to make a change to the layout template in the future, this page will not change, unless you reattach the file, but in that case your changes to the file's layout will be lost.
So the best practice from SharePoint 2003 remains - only change a file using SPD if you must. There are ways to avoid editing a file in SPD and still making the changes you thought you had to do using SPD. For example - create a new layout page template, and change the page to use that layout.
Now, I hear you asking - "what about dataviews? we can only add them using SPD!". And I will cover that in my next article on the best practice of how to add a dataview using SPD to a page, without detaching it from its layout page.
If you cannot wait until I find the time for this article, just click on the "frontpage" tag in my blog - and see how I did it with sharepoint 2003 and frontpage. The method is the same.

Saturday, April 28, 2007

Content Query Web Part, with a marquee

Meron Fridman has released a free web part - a version of the content query web part that displays the content it found as a marquee. Read about the web part in Meron's blog.
The content query with marquee webpart's properties pane - allows you to set how the marquee works.

Thursday, April 26, 2007

Announcing a new series of articles - SharePoint Designer DataViews

I see a growing amount of questions about the SPD dataviews, or questions that can be answered by "use the dataview web part", so I thought I will start a new series of articles of "how-to"s to show how to do common actions using the dataview web part.

I will start by an article on best practices of using the dataview web part - how you should approach using it. Then I will start showing examples of modifying it, using either the built-in menus, or custom xslt samples.
This is actually an upgrade to some articles I have written in the past on how to use the xslt dataview in frontpage, on this blog and in MSD2D.com.

While I am always open to suggestions on what to show, please keep in mind that I will not do your projects for you. My aim is to show how this cool feature can make your life easier, so don't bother posting complex scenarios of things you want to achieve. Simple questions like "can you explain how to use conditional formatting" or "can you give an example on how to transpose a list, so that the rows are columns" will be (probably) accepted.
Also, if you are asking for something, leave your email address (will not be published) so I can notify you when it's published.

So, keep your eyes opened on this blog (you mean you are not registered by RSS? not even by email? well, it's time!)

Content Query limitations, and Object module access rights requirements

Some new articles were published just now by Microsoft about sharepoint limits that were undocumented until now:

  1. The Content Query Web Part does not support more than 1000 lists at the same time.
    This is actually not a content query web part problem, but a SPSiteDataQuery problem, which the content query web part uses. If you are using SPSiteDataQuery in your code, or a Content Query web part in your page, you may see the error "The query cannot be completed because the number of lists in the query exceeded the allowable limit".
    So you have to be extra careful that your content query web part in not set up to query more than that amount.Read the Microsoft Article (opens in new window).
    Possible error message:
    "The query cannot be completed because the number of lists in the query exceeded the allowable limit. For better results, limit the scope of the query to the current site or list or use a custom column index to help reduce the number of lists."
  2. Error Message when running an application that accesses the object module.
    This is actually something I have encountered with my Utility pack and answered some people in the forums about this, and apperantly MS saw fit to document it properly now.
    When running an appliation on the server that tries to use the object module, you have to run with a user account that has sufficient permissions. Not just on the site, and not just on the server!:
    1. The user is a server farm administrator.
    2. The user has permissions to access the content database.
    3. The user is a site collection administrator.
    4. The user has permissions to access the Windows SharePoint Services site or the SharePoint Server 2007 site through which the code iterates.

    Notice requirement #2? this is where I failed with my utility pack on a client server - I was running the tool with an account that had local administration rights on the server, but the account didn't have any permissions on the database.
    This is also true for running stsadm operations such as backup and restore, or feature deployments... (something they don't say in the article...).
    Read the Microsoft Article (opens in new window).
    Possible error messages:
    "Unhandled Exception: System.IO.FileNotFoundException"
    "Access Denied" (The Microsoft article does not mention this, but I have seen this error for the same issue)

Wednesday, April 25, 2007

Error: "An item with the same key has already been added." When modifying the search results web part's XSLT

When you modify the xslt in the people core results web part, you may get the following error (I get it everytime) when you click "OK" in the web part settings:

System.ArgumentException: An item with the same key has already been added. 
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) 
at Microsoft.Office.Server.Search.WebControls.PeopleCoreResultsWebPart.SetSortedRefinementDataOnHiddenObject() 
at Microsoft.Office.Server.Search.WebControls.PeopleCoreResultsWebPart.GetXPathNavigator(String viewPath)
This is very annoying and I do not know how to solve it. But if you get it, don't worry - your change has been made, and you just need to check-in\publish the page to see the effect. The problem ofcourse is that you may not want to check-in the page without seeing the result first, and there is no way around this...

Monday, April 23, 2007

While I am linking, here is CAML.NET

Another cool tool - go to the the CAML.NET codeplex site and install it.
Now, why didn't I think of that?
Thanks John Holliday.

Using a SharePoint list as an authentication provider

I normally don't post links to other peoples' articles, but when something makes me say "wow" or "why didn't I think of that". This tip is really cool.
It shows in an easy to follow step-by-step process how to make a "users" list in sharepoint, and then make sharepoint (or any other .net application) authenticate users based on that list.
The possibilities are endless. Thank you Willie Rust.