Filed under: MOSS
In this post I show how you can create a content type programmatically and then create a new site column and add it to your new content type.
using(SPSite site = new SPSite(urlToSite))
{
using(SPWeb web = site.OpenWeb())
{
SPContentType contentType =
new SPContentType(web.AvailableContentTypes["Parent Name"],
web.ContentTypes, “Name”);
// use true below to make required field
web.Fields.Add(“NameOfColumn”, SPFieldType.TypeOfField, true);
SPFieldLink fieldLink = new SPFieldLink(web.Fields["NameOfColumn"]);
contentType.FieldLinks.Add(fieldLink);
contentType.Update();
}
}
Filed under: MOSS
I could write an article about how to create a custom web service for WSS 3.0/MOSS/SharePoint, but why do so when there is already a greate article on MSDN?
The article does not go any deeper than exposing the typical Hello World web service, but it will certainly get you off and running.
Filed under: MOSS
Recently I got a request from a client to get a report of the number of items they had in the document libraries on their site. They wanted it broken down with the number of files per site, document library, and eventually per folder. Anyways its a fairly simple procedure (or so I think anyways), but figured I’d post it up since its been a while since I’ve thrown anything up. The code is broken into two methods, the first is used to iterate through all the child webs of a given SPWeb and also iterates through all the lists for that web and then calls the second method to interate through all the folders in a given list.
public void CrawlWeb(SPWeb web)
{
foreach (SPList list in web.Lists)
{
CrawlFolder(list.RootFolder);
}
foreach (SPWeb subWeb in web.Webs)
{
CrawlWeb(subWeb);
}
}
public void CrawlFolder(SPFolder folder)
{
if (folder != null && folder.Name.CompareTo(“forms”) != 0)
{
foreach (SPFolder subFolder in folder.SubFolders)
{
CrawlFolder(subFolder);
}
}
}
Filed under: MOSS
This is a quick post on how to add another value to a Multi-choice field using the object model. The code is below and assumes that you already have a reference to your SPWeb object stored in ‘myWeb’.
SPFieldMultiChoice myField = (SPFieldMultiChoice)myWeb.Fields["Field Name"];
myField.Choices.Add(“Value to add”);
myField.PushChangesToLists = true; //Set to false if you don’t want Lists/Content types to update automatically
myField.Upate();
Filed under: MOSS
What do you do, if you have an event receiver which you want to attach to one and only one document library within a SPWeb? Using the infastructure provided to you by the feature framework you can attach it to a SPWeb as whole, but not a specific list. Fortunately pretty much everything can be done using the object model. The code below shows how to attach an event receive to a specific list. This code assumes that the assembly containing the event receiver has been deployed to the GAC and that the feature has already been installed through stsadm.
SPSite mySite = new SPSite(“http://localhost/“);
SPWeb myWeb = mySite.OpenWeb();
SPList myList = myWeb.Lists["Name of List"];
SPEventReceiverDefinitionCollection myReceivers = myList.EventReceivers;
SPEventReceiverDefinition receiver = receivers.Add();
receiver.Name = “My Receiver Name”;
receiver.Assembly = “<namespace>, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxx”;
receiver.Class = “<namespace>.<class>”;
receiver.Type = SPEventReceiverType.ItemAdding;
receiver.Update();
Filed under: MOSS
The title of this post is an error message which confronted me when I first attempted to use the Document Conversion Service available in MOSS; attempting to create such a library on your site does not solve the problem. The error message appeared after I had activated the Document Conversion Service in Central Administration and then attempted to convert my first document.
The solution around this issue is actually rather basic. Simply follow these steps:
- On your site go to site actions -> site settings
- Under Site Administration click on Site features
- Activate Office SharePoint Server Publishing
Just don’t forgot to configure the converter settings otherwise you’ll get more error messag when you try to convert a document!
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")]