Filed under: MOSS
This post is meant to just be a personal reference. Every so often I forget what .NET data type SharePoint stores in list columns so this is just meant to be a reference.
| Column Type | .NET Type |
|---|---|
| Single Line of Text | System.String |
| Multiple Lines of Text | System.String |
| Choice | System.String |
| Number | System.Double |
| Currency | System.Double |
| Date and Time | System.DateTime |
| Lookup | System.String |
| Yes/No | System.Boolean |
| Person or Group | System.String |
| Person or Group (multiple choices) | Microsoft.SharePoint.SPFieldUserValueCollection |
| Hyperlink or Picture | System.String |
| Calculated | Another column type is selected upon column creation |
Filed under: MOSS
So I recently discovered how to modify the division and region options presented in a Site Directory page.
While on the Site Directory page do the following:
- Click on View All Site Content
- Click on the Sites list.
- On the Sites list page, go to the list settings (Settings->List Settings)
On the list settings page you’ll notice that there are two columns listed named Division and Region and both are typed as choice. You can edit the choice values to change the Region and Division options!
Filed under: MOSS
Just going to drop this in here, but it is also possible to extend the web part I wrote about earlier that would render a specific view of a list. This extension allows that view of the list to be querried against. The modification is fairly simple instead of calling SPView.RenderAsHtml() we’ll end up calling SPList.RenderAsHtml(SPQuery q). In order to do this you need to insert two lines of code between where we instantiated our view and when we got the html. In the end your two lines will now be four:
SPViewmyView = myList.Views[viewName];
SPQuerymyQuery = new SPQuery(myView);
myQuery.Query = “a CAML query goes here”;
html = myList.RenderAsHtml(myQuery);
Then you just have to replace the string “a CAML query goes here” with an actual CAML query. I should probably also mention that CAML querries can be made simple with U2U’s CAML Builder. Keep in mind that the web part can be further powerful by leveraging class properties.
Filed under: Uncategorized
So you have a web part, but want it to be generic so that it can be reused in multiple places. So you do the standard thing add in some properties to your web part class and expose them via get and set, for example:
private string myString;
public string MyString
{
get { return myString; }
set { myString = value; }
}
However in order to enable this property to be useful in making a generic web part you need to decorate it with two properties:
- [WebBrowsable(true)]
- [Personalizable(PersonalizationScope.Shared)] – You can also set the personalization scope to User if you want users to be able to customize the web part themselves
Now you’ve got the functionality that you want, but you can take it further still. More than likely you will not be the only person using your web part. So have the property with the name “MyString” isn’t a very user friendly experience, most people will probably question why there is no space. In addition do you really want to rely on the name of the property to communicate exactly what it is used for? No, you probably want to have a description for the user as well. With that in mind you can also add the next two decorations:
- [WebDisplayName("My String")]
- [WebDescription("This is the description of my property.")]
Then one final thought, all the properties you define within your class will categorized as miscellaneous. Is that really where you want your property? What if you have several properties that aren’t directly related? In that case you might want to put them in different categories, for this you can use this decoration:
- [System.ComponentModel.Category("My Category")]
Filed under: MOSS
This is fairly and more than likely widely known, but for those that are new on the SharePoint scene then this post is about how to build a web part which will render a list. You might already be asking why make a post about this when the web part is provided out of the box? Well take a look at the code and think of how you can extend it to make it more useful. What if you want to display a list which is not on the current SPWeb? Anyways here is the code, enjoy! (though I believe the only people coming by this blog are looking at the poorly worded post on SPNavigation; some day maybe I’ll do a better post on that subject since it seems to be attracting people).
public class ListView : System.Web.UI.WebControls.WebParts.WebPart
{
private string html;
protected override void OnPreRender(EventArgs e)
{
using (SPSite mySite = SPContext.Current.Site)
{
SPWeb myWeb = mySite.OpenWeb();
SPList myList = myWeb.Lists[listName];
SPView myView = myList.Views[viewName];
html = myView.RenderAsHtml();
this.Title = listName; // Changes title of web part
}
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write(html);
}
}
Filed under: MOSS
Just as the title says a quick code snippet to create a new item in a document library which uses the default template for the library.
SPWeb myWeb = SPContext.Current.Site.OpenWeb();
SPDocumentLibrary myLib = myWeb.Lists["Document library name"];
SPFolder myFolder = myLib.RootFolder;
SPFile myFile = myWeb.GetFile(myLib.DocumentTemplateUrl);
myFolder.Files.Add(“File name.extension”, myFile.OpenBinary(), true);
Only note is to change the last argument of the Add() call to false if you don’t want to overwrite an existing document in the library that shares the same name.
Filed under: MOSS
UPDATED (05/03/07 ): Corrected problem where the list would not sort properly.
I was working on a project earlier today where the customer in question had requested a list so that they could track various projects. One of the things they wanted when all was said and done was to be able to see a summary view of all the projects and their value. In order to make the list more valuable I set about creating a custom view would would group projects together by departments and categories. Then I got to thinking that being able to group them by due date, specifically which are due every month would also be interesting. This is when I began to explore calculated values which I began to see did not have a whole lot documentation that I was able to find. Ultimately what I wanted was to have the projects grouped by month and then have this list of groups sorted by furthest in the future to furthest in the past. To do so I ended up using the following formula:
=IF(YEAR([Due Date])=1899,”No Due Date Set”,YEAR([Due Date])&” “&IF(MONTH([Due Date])<10,”0″&MONTH([Due Date]),MONTH([Due Date]))&” “&TEXT([Due Date],”mmmm”))
So in broad terms this is a simple if statement. If the due date year is 1899 return “No Due Date Set” otherwise return this other value. The reason for the 1899 is that it would seem that if no date is entered for a list item it defaults the date to December 1899 (I believe it sets the day of the month to the 31st). So if there’s no due date then let’s indicate that and group those together, better than claiming they were due 108 years ago! Now the second part, well YEAR([Due Date]) will give us back a numeric value of the year. Now the nested if statement is going to ultimately gives us back the numeric representation of the month. The reason for needing the if is that if the month is January-September then we need to pad the numeric value with a zero, otherwise we won’t end up with the correct order of groups.The Text() function will end up giving us back a string representation of the month (if I had used “mm” as the second argument I would have gotten back a numeric value). So if my due date was June 1st, 2007 then I would end up with the string “2007 6 June”.
Now why would I want both the numeric and string value of the month? The answer is pretty simple for that, sorting! If I have project due in both April, May, and June then without the numeric value my list would be ordered as follows “2007 April”, “2007 June”, “2007 May” (or the opposite depending upon which way you’re sorting). Well that doesn’t work, but if we add in the numeric value then we’ll end up with the correct order: “2007 4 April”, “2007 5 May”, “2007 6 June”. Not the greatest of methods I know (and a cheap hack), but it works.
Now to see if I can modify it so that we get proper sorting, but don’t have to see the numeric value for the month.