Thursday, August 21, 2008

Reset functionality for InfoPath

Something I really miss in InfoPath 2007 is the possibility to have a simple "reset" button, one that clears all your fields and lets you start all over. I didn't find any out-of-the-box functionality for this, so I wrote some code to make this work. So if you're looking for something that clears all your datasource's fields, try this piece of code:

private void ClearAllFields()
{
   //Create a navigator and select the first group in your datasource
   XPathNavigator nav = this.MainDataSource.CreateNavigator();
   XPathNodeIterator nodes = nav.SelectSingleNode("/my:myFields/my:group1", NamespaceManager).SelectChildren(XPathNodeType.Element);

   ClearLevel(nodes);
}

private void ClearLevel(XPathNodeIterator nodes)
{
   while (nodes.MoveNext())
   {
      if (nodes.Current.HasChildren)
      {
         ClearLevel(nodes.Current.SelectChildren(XPathNodeType.All));
      }

      if (nodes.Current.NodeType == XPathNodeType.Text)
      {
         //Reset the value to an empty string
         nodes.Current.SetValue("");
      }
   }
}

Just put the ClearAllFields() method in your button.clicked event handler and it should all work just fine! Make sure you select the right node to start with. Replace the "/my:myFields/my:group1" with the path to your top level group.

Friday, June 27, 2008

The specified name is already in use.

If the above error looks familiar and you can't seem to get rid of it, then read on, I might have a solution for you then!

When trying to activate a feature that would create a list, I received the following error:

The specified name is already in use.

A list, survey, discussion board, or document library cannot have the same name as another list, survey, discussion board, or document library in this Web site.
Use your browser's Back button, and type a new name.


The biggest problem was that the list really didn't exist. When I went to "All Site Content", nothing was there with the name of the list I was creating. I also looped over all my lists with three lines of code I quickly wrote, but nothing there either... After some searching on our good friend Google, I learned that apparently, if you open the site with SharePoint Designer, there is a folder there with that name. Just delete the folder and everything works fine again...

But what if you don't have SharePoint Designer installed and you can't intall it immediately, like me? Well, then these few lines of code might help you out:

SPSite site = null;
SPWeb web = null;

using (site = new SPSite(siteURL))
{
    using (web = site.OpenWeb())
    {
        web.Folders[nameOfListToCreate].Delete();
    }
}

Thursday, April 3, 2008

The file manifest.xml does not exist in the solution package

It's been a while since my last post (I'd really need to write that post on how to integrate Dynamics AX into MOSS ) and this one will also just be a quickie...

When trying to add my solution to the solution store, I got the error The file manifest.xml does not exist in the solution package. This was weird, because I didn't change anything to the deployment parts of my solution and it had always worked before... Also, the manifest.xml file seemed to be inside the wsp file just fine. Thanks to this blog, I was able to solve it!

Apparently, you get this error the moment your package gets too big. Mark Beij mentions 1440kb, but with me it already occured when my package got over 1400kb. The solution is to add the following lines to the top of your cab.ddf file:

.Set CabinetFileCountThreshold=0
.Set FolderFileCountThreshold=0
.Set FolderSizeThreshold=0
.Set MaxCabinetSize=0
.Set MaxDiskFileCount=0
.Set MaxDiskSize=0


Now that all the size limitations are off, just make your wsp file again and you should be able to add your solution again.

Friday, March 7, 2008

Filtering on multiple values with SPGridView

If you want to show list data in a Web Part, one of the options to consider is using the SPGridView control. There is a nice article on Bob's SharePoint Bonanza on how to set this up. This really is a great article, but it won't allow you to filter on multiple columns. My customer wanted to be able to do this though and he also wanted the Web Part to remember the filtering and sorting options when leaving the page.

I was able to make this work using custom Web Part properties. Here's how to do it:

- First, set up your SPGridvVew and it's datasource as described in Bob's article. When you're done, also add a private string to your Web Part. This string will remember the previous filter.

private string prevFilter = "";

Next, find the following line in the SetupObjectDataSource method:

dataSource.FilterExpression = (string)ViewState["FilterExpression"];

Below this line, add the following line:

prevFilter = dataSource.FilterExpression;

- Your next step is to create two custom properties: one for the filter settings and one for sorting.

private string userSettingsFilter;
private string userSettingsSort;

///


///Hidden property to remember filtering options set by the user
///

[Browsable(false), Category("Advanced"),
DefaultValue(""),
Personalizable(PersonalizationScope.User),
FriendlyName("User Settings Filter"), Description("Hidden user settings for filtering.")]
public string UserSettingsFilter
{
get
{
return userSettingsFilter;
}
set
{
userSettingsFilter = value;
}
}

///
///Hidden property to remember sorting options set by the user
///

[Browsable(false), Category("Advanced"),
DefaultValue(""),
Personalizable(PersonalizationScope.User),
FriendlyName("User Settings Sort"), Description("Hidden user settings for sorting.")]
public string UserSettingsSort
{
get
{
return userSettingsSort;
}
set
{
userSettingsSort = value;
}
}


- In the OnPreRender method, you first check if there is a sort expression set. If this is the case, then save it. Next, check the filter that has been saved to the ViewState. Depending on the column title (is there already a filter on that column or not), save the filter setting to the custom filter property by appending it with either " and " or " or ".

protected override void OnPreRender(EventArgs e)
{
//Check if there is a sort expression set. If this is the case: save it.
if (gridView.SortExpression != "")
{
userSettingsSort = gridView.SortExpression + "&" + gridView.SortDirection;
}

//Get the filter expression into the viewstate. If a filter is set, add it to the saved filter expression.
ViewState["FilterExpression"] = dataSource.FilterExpression;
if (prevFilter != string.Empty && ViewState["FilterExpression"].ToString() == string.Empty)
{
userSettingsFilter = "";
}
if (ViewState["FilterExpression"].ToString() != "")
{
if (userSettingsFilter != null)
{
if (userSettingsFilter != "")
{
//If there already is a filter on a certain column and the user adds another value of this column,
//add it with an "or" expression
if (userSettingsFilter.Contains(ViewState["FilterExpression"].ToString().Substring(0, ViewState["FilterExpression"].ToString().IndexOf('=') + 1)))
{
int tempIndex = userSettingsFilter.IndexOf(ViewState["FilterExpression"].ToString().Substring(0, ViewState["FilterExpression"].ToString().IndexOf('=') + 1));
userSettingsFilter = userSettingsFilter.Insert(tempIndex, "(");
string tempSettings = userSettingsFilter.Substring(tempIndex);
if (tempSettings.Contains(" and "))
{
userSettingsFilter = userSettingsFilter.Insert(tempIndex + tempSettings.IndexOf(" and "), " or " + ViewState["FilterExpression"] + ") ");
}
else
{
userSettingsFilter = userSettingsFilter.Insert(tempIndex + tempSettings.Length, " or " + ViewState["FilterExpression"] + ") ");
}
}
else
{
userSettingsFilter = userSettingsFilter + " and " + ViewState["FilterExpression"].ToString();
}
}
else
{
userSettingsFilter = ViewState["FilterExpression"].ToString();
}
}
else
{
userSettingsFilter = ViewState["FilterExpression"].ToString();
}
}

//Save the changed properties
this.SetPersonalizationDirty();
base.OnPreRender(e);
}


- Next, in the Render method, use the saved filter settings on the datasource's FilterExpression. If a sort setting has been saved, sort the gridview either ascending or descending (depending on the text in the saved setting) on the saved column.

protected override void Render(HtmlTextWriter writer)
{
dataSource.FilterExpression = userSettingsFilter;

if (userSettingsSort != null)
{
if (userSettingsSort != "")
{
if (userSettingsSort.ToLower().Contains("ascending"))
{
gridView.Sort(userSettingsSort.Substring(0, userSettingsSort.IndexOf('&')), SortDirection.Ascending);
}
else
{
gridView.Sort(userSettingsSort.Substring(0, userSettingsSort.IndexOf('&')), SortDirection.Descending);
}
}
}

gridView.DataBind();
base.Render(writer);
}


There are a few disadvantages when using this method though. Your list won't show icons showing which columns are being filtered on and you will only get the option to remove the filter you've last set. Due to this, removing that last filter value, will automatically remove all filters. Also, if you go away from the page and then return, you won't have the option to undo the filter and you will have no indication that there is a filter enabled (except for the fact that not all fields are shown). To remove this filter, you need to set an extra filter and then remove it again, so all filter options are cleared. Another disadvantage is when you set your filter options in a certain way that no data is shown, your heading will disappear and you won't be able to clear the filter anymore, so you'll be stuck with an empty SPGridView forever. To solve this, you can either make the custom filter property visible, so you can edit your Web Part and manually remove the filter, or you can add a button to the Web Part that clears the filter for you.

A better way of implementing this is rendering the view as html (see one of my previous posts). This way, the list will have the same look and feel as a default SharePoint list. I think that this last solution is better in almost every way, but for some situations you might need to use the SPGridView, for example when you need to use a certain datasource (which was the situation I was in, it was impossible for me to display the same data that I got from the datasource) and with this implementation, you're also able to filter on multiple values of one column, which is not possible with the default SharePoint list.

If you have any comments, remarks, possible improvements or questions, just shoot!

Monday, February 25, 2008

Custom Web Part properties not showing

Just a quick note: if you're having trouble getting your custom properties to show when writing your own Web Part, check which class you're inheriting from. If your Web Part inherits System.Web.UI.WebControls.WebParts.WebPart, try changing this into Microsoft.SharePoint.WebPartPages.WebPart and then test your properties again, they may just work now... (if you haven't made any other mistakes of course ;-))

Update: apparently, you can also get this to work when inheriting from System.Web.UI.WebControls.WebParts.WebPart. To do this, change the line "WebPartStorage(Storage.Personal)" (above your properties) to "Personalizable(PersonalizationScope.User)". If you were using "this.SaveProperties = true" to save the changes done to your properties, this won't be available when inheriting from the System.Web.UI.WebControls.WebParts.WebPart class. You can use this.SetPersonalizationDirty() instead.

Thursday, February 21, 2008

Rendering a list programmatically

Today, I was trying to display list data in a Web Part programmatically and have it behave the same way as the default SharePoint lists do. I was given a Web Part that was using an SPGridView to do this, but the filtering in it didn't work the way it was supposed to and it also didn't show which column it was filtered on (as default SharePoint lists do).

Thanks to this post, I was able to render the list the same way default lists get rendered. Here's an example on how to do it for the tasks list, including an example Caml query that sorts the view by title and filters it so that it shows only the incomplete tasks assigned to the current user. Beware though: when using this Caml query, sorting and filtering won't work since it will always render the view by the conditions you have provided in your query. Still working on solving this...

private string html;

protected override void OnPreRender(EventArgs e)
{
using (SPSite mySite = SPContext.Current.Site)
{
using (SPWeb myWeb = mySite.OpenWeb())
{

SPList myList = myWeb.Lists["Tasks"];
SPView myView = myList.Views["All Tasks"];

SPQuery myQuery = new SPQuery(myView);

myQuery.Query = "<OrderBy><FieldRef Name=\"Title\" /></OrderBy><Where><And><Eq><FieldRef Name=\"AssignedTo\" /><Value Type=\"User\"><UserID/></Value></Eq><Neq><FieldRef Name=\"Status\" /><Value Type=\"Choice\">Completed</Value></Neq></And></Where>";

html = myList.RenderAsHtml(myQuery);

this.Title = "Tasks";

}
}
}

protected override void Render(HtmlTextWriter writer)
{

writer.Write(html);

}

Tuesday, January 22, 2008

“This site is not found in the SharePoint configuration database.” when registering a site in Dynamics Ax

At the moment, I am working on integrating MOSS and Dynamics Ax, so you can expect some updates on this topic the next few weeks.

If you want to use the out-of-the-box integration, you can either create SharePoint sites using the Dynamics Enterprise Portal site template, or you can register the site in Ax to be able to use the Dynamics Web Parts in your existing SharePoint site. For my situation, I needed this last option, but when testing this, I regularly got the next error:

Error:
This site is not found in the SharePoint configuration database.


The solution for this problem may or may not be very simple... If you're lucky, just remove the slash at the end of the url and you shouldn't get the error anymore. With other sites, this won't work though and I haven't found the solution to this either, so if you were able to solve it, feel free to let me know how you did it!

Update: Also check your casing. This seemed to work in my situation…

Monday, January 14, 2008

Error: “Cannot find the path specified” when adding role assignments

If you ever get the error “The system cannot find the path specified. (Exception from HRESULT: 0x80070003)” while adding role assignments to a library or web in SharePoint, then this might help you solve your problem… I was trying to add role assignments to a library (using library.RoleAssignments.Add(newRole)), which had already worked perfectly before, but when trying it this time, I got the error I’ve mentioned before. The key to solving my issue was something that didn’t include paths at all: I just forgot to break the role inheritance of my library. So if you ever encounter this error, just add the following lines, they will most probably solve your issue:

library.BreakRoleInheritance(true);
library.Update();