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: 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);
}
}