If you're trying to deploy some custom site columns using a feature and the feature activates fine, but the columns don't show up and your logs give you the above error, then the solution is pretty simple: when you're defining your columns, you have to make sure to use brackets around your column guid, like this: {GUID}. Deploy your feature again, deactivate it, activate it again and everything should work fine! You also have to use the brackets when you're referencing the column in a content type!
Thanks to Edwin Vriethoff for posting this!
Wednesday, September 23, 2009
Tuesday, September 22, 2009
Modify the default scope for your search box
Today I got asked how to modify the search box on a MOSS site so that it displayed "People" by default in stead of "All Sites". The customer already figured out a way to do this with code, but they weren't 100% sure about it. I had to search for this myself too, cause to be honest, I hadn't done this before, even though I'm already working with SharePoint for almost 3 years now... That's why I'm making this short post, so that all the other people who are trying to do this, know how easy it really is.
To modify the default scope, go to your home page and navigate to "Site Actions” --> “Site Settings” --> “Modify All Site Settings". On this page, pick "Search scopes" under the Site Collection Administration. Here you'll see some different "Display groups", one of them is the "Search Dropdown", which is the search box you see on your SharePoint site. If you click on the "Search Dropdown" link, you'll be taken to a page with some settings for this group and the bottom one is where you set your default scope. So modify this to whatever you want, click OK and you're done!
To modify the default scope, go to your home page and navigate to "Site Actions” --> “Site Settings” --> “Modify All Site Settings". On this page, pick "Search scopes" under the Site Collection Administration. Here you'll see some different "Display groups", one of them is the "Search Dropdown", which is the search box you see on your SharePoint site. If you click on the "Search Dropdown" link, you'll be taken to a page with some settings for this group and the bottom one is where you set your default scope. So modify this to whatever you want, click OK and you're done!
Thursday, August 6, 2009
The path specified cannot be used at this time
When I opened my IIS manager (on a server with SharePoint 2007 installed) today and tried to connect to the localhost (which had always been possible in the past, without any changes done to the server), I suddenly got the error "The path specified cannot be used at this time". The solution for this is quite simple luckily... Just go to the services on your server and restart the Windows SharePoint Services Timer service. Then refresh your IIS manager and everything should be back to normal again!
Wednesday, June 24, 2009
Service stuck in "stopping" state
A couple of days ago I wanted to restart the Windows SharePoint Services Timer service on one of our SharePoint servers because some timer jobs seemed to be stuck. When I tried this though, the state of the service became "stopping" and it stayed that way for hours. I tried stopping it with NET STOP, but this gave me an error that the service "could not be controlled in its current state". I read online that rebooting didn't help with most people and this also wasn't an option since this was a live server, but then I read about PsTools. This set of command line tools contains a little something called PsKill. This little tool kills the process for you, allowing you to start it again. In my case, I used it the following way:
PsKill.exe \\moss-server owstimer
This killed it immediately and I was able to start the service again.
You do have to be careful with this though, since not all services can be killed without any risk (for example when a process is writing things to a database).
One last thing, for your convenience: the link to PsTools: http://download.sysinternals.com/Files/PsTools.zip.
PsKill.exe \\moss-server owstimer
This killed it immediately and I was able to start the service again.
You do have to be careful with this though, since not all services can be killed without any risk (for example when a process is writing things to a database).
One last thing, for your convenience: the link to PsTools: http://download.sysinternals.com/Files/PsTools.zip.
Thursday, June 18, 2009
OWSTIMER error when restoring a SharePoint site
When I was trying to restore a web application with a single site in it and one content database, I received the following two errors:
Error: Object OldIntranet (89) (previous name: Intranet (80)) failed in event OnRestore. For more information, see the error log located in the backup directory.
SPUpdatedConcurrencyException: An update conflict has occurred, and you must re-try this action. The object SPWebApplication Name=OldIntranet (89) Parent=SPWebService is being updated by svc_moss_sql, in the OWSTIMER process, on machine MP-MOSS-INDEX01. View the tracing log for more information about the conflict.
Error: Object SharePoint _OldIntranet89_Content (previous name: SharePoint _Intranet_Content) failed in event OnPostRestore. For more information, see the error log located in the backup directory.
SPException: Cannot attach database to Web application. Use the command line tool or Central Administration pages to attach the database manually to the proper Web Application.
I tried every possible solution I found on the internet, but nothing worked. Then I decided to try the update with stsadm through the following command:
stsadm -o restore -directory "\\fileshare\backupfolder" -restoremethod new -item
"Farm\Windows SharePoint Services Web Application\Intranet (80)"
After doing this, I first got a new, different error about the Administration Service. I restarted this service and after that, the stsadm command also gave me the two previous errors. So then I decided to just turn off the Windows SharePoint Services Timer service... and what do you know, it worked! Afterwards, I also noticed that I had turned off the WSS Administration service, so if turning off the Timer service doesn't work, you can also try this one.
I do know that turning off the Timer service in a live environment probably is not such a good idea, but well, if nothing else works, you might be forced to do so...
I also didn't test doing the restore through Central Administration (since it had already been succesfully restored through stsadm), but feel free to give it a shot and let me know if it also works! (Although I doubt it, since I think the procedure through the Central Admin uses a timer job for this, which won't work if the Timer services has been stopped.)
Error: Object OldIntranet (89) (previous name: Intranet (80)) failed in event OnRestore. For more information, see the error log located in the backup directory.
SPUpdatedConcurrencyException: An update conflict has occurred, and you must re-try this action. The object SPWebApplication Name=OldIntranet (89) Parent=SPWebService is being updated by svc_moss_sql, in the OWSTIMER process, on machine MP-MOSS-INDEX01. View the tracing log for more information about the conflict.
Error: Object SharePoint _OldIntranet89_Content (previous name: SharePoint _Intranet_Content) failed in event OnPostRestore. For more information, see the error log located in the backup directory.
SPException: Cannot attach database to Web application. Use the command line tool or Central Administration pages to attach the database manually to the proper Web Application.
I tried every possible solution I found on the internet, but nothing worked. Then I decided to try the update with stsadm through the following command:
stsadm -o restore -directory "\\fileshare\backupfolder" -restoremethod new -item
"Farm\Windows SharePoint Services Web Application\Intranet (80)"
After doing this, I first got a new, different error about the Administration Service. I restarted this service and after that, the stsadm command also gave me the two previous errors. So then I decided to just turn off the Windows SharePoint Services Timer service... and what do you know, it worked! Afterwards, I also noticed that I had turned off the WSS Administration service, so if turning off the Timer service doesn't work, you can also try this one.
I do know that turning off the Timer service in a live environment probably is not such a good idea, but well, if nothing else works, you might be forced to do so...
I also didn't test doing the restore through Central Administration (since it had already been succesfully restored through stsadm), but feel free to give it a shot and let me know if it also works! (Although I doubt it, since I think the procedure through the Central Admin uses a timer job for this, which won't work if the Timer services has been stopped.)
Wednesday, March 18, 2009
Workflow task: Value does not fall within the expected range
If you get the above error when trying to open a task form in a workflow, then read on!
Here's the situation where I got this error: I have a custom approval workflow where one user adds an order request through an InfoPath form. When the form gets submitted, the "IT Approval" group gets assigned the task to approve or reject the order. Also important is that security has been broken so that only the right people can see and approve the tasks. The whole thing is a bit more elaborate than this, but this is basically all you need to know to understand the problem and hopefully recognize it...
Now, if someone in the IT Approval group goes to check his "My Tasks" page, he sees all the tasks assigned to the group. So far, so good... But when he tries opening a task, he gets an error page displaying the "Value does not fall within the expected range" error message.
I've tried many different things, making sure the IT Approval group had contribute access to the task, read access to the site, the task list and the workflow history, but nothing seemed to work. However, there was one place that I missed: the original document.
So if you're facing this problem and you're using custom security, check your InfoPath form (or any other kind of document that triggered the workflow) and make sure the approval user has read access to this file too. I don't know the exact reason for this, but I guess this is because the workflow is copying fields from the original file into the task form and apparently, this only works when the user can read the original file too.
Here's the situation where I got this error: I have a custom approval workflow where one user adds an order request through an InfoPath form. When the form gets submitted, the "IT Approval" group gets assigned the task to approve or reject the order. Also important is that security has been broken so that only the right people can see and approve the tasks. The whole thing is a bit more elaborate than this, but this is basically all you need to know to understand the problem and hopefully recognize it...
Now, if someone in the IT Approval group goes to check his "My Tasks" page, he sees all the tasks assigned to the group. So far, so good... But when he tries opening a task, he gets an error page displaying the "Value does not fall within the expected range" error message.
I've tried many different things, making sure the IT Approval group had contribute access to the task, read access to the site, the task list and the workflow history, but nothing seemed to work. However, there was one place that I missed: the original document.
So if you're facing this problem and you're using custom security, check your InfoPath form (or any other kind of document that triggered the workflow) and make sure the approval user has read access to this file too. I don't know the exact reason for this, but I guess this is because the workflow is copying fields from the original file into the task form and apparently, this only works when the user can read the original file too.
Thursday, March 12, 2009
Debugger: No symbols have been loaded
Just a quick post that can save you a lot of time.... I was debugging a SharePoint workflow that had already been deployed and everything, but it needed some extra changes. I never succeeded in debugging it though and it always gave me the "no symbols have been loaded for this assembly" message. After trying almost everything (restarting the server, rebuilding and deploying everything, clearing the .Net cache, ...), I found a post of someone saying something about "debug mode". Turned out my Visual Studio project was set in "Release" mode by the one who installed the application and you can't debug when it's in Release mode. To reset this, simply right click on your solution name, choose properties, click on "Configuration Properties" and under "Configuration", select "Debug" for all your projects. Then rebuild and deploy again (make sure you use the .dll files from the "debug" directory in stead of the "release" directory) and everything should debug fine again...
Labels:
debug mode,
Debugging,
No Symbols,
release mode,
SharePoint
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.
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();
}
}
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.
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!
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!
Labels:
Custom properties,
filtering,
multiple values,
SharePoint,
SPGridView
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.
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);
}
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…
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…
Labels:
Axapta,
Dynamics Ax,
MOSS,
register SharePoint site in Ax
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();
library.BreakRoleInheritance(true);
library.Update();
Labels:
Cannot find the path,
Role Assignments,
SharePoint
Subscribe to:
Posts (Atom)
