Posts

Showing posts from December, 2007

Trying to use an SPWeb object that has been closed or disposed and is no longer valid error

I have encountered this error ("Trying to use an SPWeb object that has been closed or disposed and is no longer valid.") at 2 different location. The first time, I was trying to dispose the SPSite object that I got from SPContext.Current.Site. According to this post , you shouldn't dispose the SPSite or SPWeb object if it's from the SPContext. You can refer to some of the best practices for disposing sharepoint objects here . So, to resolve this problem, you can either not dispose your object or you can create the SPSite/SPWeb object using a different way and dispose it later. i.e. using (SPSite site = new SPSite ( siteUrl )){}. The 2nd time that I got this error message, I didn't know what was going on and it took me a long time to find out the cause of the problem. I was using PublishingWeb.IsPublishingWeb(SPWeb) method to check if a particular web is a publishing web. After I checked and it is publishing web, I tried to update a list item and then I got t

Customize EditingMenu, Quick Access, and Site Action

Editing Menu, Quick Access, and Site Action are three of the menu bar in MOSS that are customizable. In C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\EditingMenu, you can find 3 xml files corresponding to each of the menu. Those 3 xml files (EditingMenu.xml, QuickAccess.xml, and SiteActioni.xml) are applied to all MOSS site on the Web Front End server. To customize for just a site collection, you can customize the xml files in the Editing Menu folder in the master page gallery (/_catalogs/masterpage). See more details here . *Note: After each change to the xml file, an application pool recycle or iisreset has to be done before the changes can take effect on the site. The following are the possible errors that you can get when you customize these xml files: "Index was outside the bounds of the array." - There are a lot of causes for this issue. In my case, I set UseResourceFile="true" while I actually typed in the text dire

How to Customize MOSS Site Manager (Site Content and Structure Page)

MOSS Site Manager page (Site Content and Structure page, _layouts/sitemanager.aspx) in my opinion one of the most important page for Web Content Management in MOSS. However, it is not so easy to custom. As you might be aware that the custom ECM menu (that shows on normal library page) won't be showing up in this page. This post hopefully will provide a starting point for you. To customize it, I did the following things: copy the original siteManager.aspx in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS and create a new file, call CustomSiteManager.aspx. Change the SiteAction.xml in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\EditingMenu if you want to customize for all sites on MOSS environment. Or you can change the customSiteAction.xml at Master Page Gallery -> Editing Menu folder if you only want one site collection to be customized. Basically, you want to point the "Manage Cont

Debugging and Trouble Shooting in WSS3.0 and MOSS

The followings are a few things that you can use to debug and trouble shoot your custom code in MOSS development environment. Out-Of-The-Box (OOTB) SharePoint logs: The log can be located @ C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\LOGS. The detail level of the information can be setup in the Central Administrator. There are a lot of information in those log files. Fortunately, there are tools out there to help you parse and filter the logs (i.e. SpsDev.com ULS Log Reader). Sometimes you'll get unexpected error has occurred on a web page and there is no other information to help you debugging this issue. There is a great post that shows you how you can get rid of the useless unexpected error page and get the standard ASP.net error page for debugging. Change the following line in the web.config <SafeMode MaxControls=“200“ CallStack=“false“… to <SafeMode MaxControls=“200“ CallStack=“true“… and you'll also need to set custom errors to 'Off&

Adding Field to Content Type Programmatically

The following sample code add field to the content type programmatically. The field has to be created as site column first refer to my first blog for how to add field programmatically . It's not really the field that's added to the content type but a reference of the field (It's the same as fieldRef attribute in feature elements xml). SPSite site = new SPSite ( _siteUrl ) PublishingSite pubSite = new PublishingSite(site); SPContentType contentType = pubSite.ContentTypes[ contentTypeName ]; SPField field = pubSite.RootWeb.Fields[ fieldGuid ]; SPFieldLink fieldLink = new SPFieldLink(field); contentType.FieldLinks.Add(fieldLink); contentType.Update(true); Later on if you want to update the FieldLinks (fieldRef), you can get the FieldLink for the field by the same fieldGuid. The following code sample shows how to return the SPFieldLink from content type. foreach (SPFieldLink fieldRef in contentType.FieldLinks) { if (fieldRef.Id == fieldGuid) return fieldRef; }

Create a New Welcome page programmatically

The following code sample creates a new publishing page in a sub site in MOSS and set it to be the welcome page for the sub site. By providing the pagelayout, it knows which content type to use to create the publishing page. private void CreateNewWelcomePage(SPWeb web, PageLayout pageLayout) { if (PublishingWeb.IsPublishingWeb(web)) { PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web); PublishingPage welcomePage = pubWeb.GetPublishingPages().Add("Welcome.aspx", pageLayout); welcomePage.Title = "Welcome Page"; welcomePage.Update(); welcomePage.CheckIn("Initial version"); pubWeb.DefaultPage = welcomePage.ListItem.File; pubWeb.Update(); } }

Create MOSS Site Columns Programmatically

There a few ways to create site column programmatically in MOSS. One of the way is to use AddFieldAsXml(). Using this method give you the ability to create field with a pre-defined GUID. There is a set of minimum attributes that are required in this xml. The following is the sample code. string siteUrl=http://testingportal.com; string fieldXml = ""; string fieldGuid="{21C910B8-BA41-413a-A87E-D0D05AC4A5A8}"; string fieldInternName="customField"; fieldXml = String.Format("<field id="\" group="\" name="\" format="\" type="\">", fieldGuid, fieldInternalName); using (SPSite site = new SPSite (siteUrl)) { using (SPWeb web = site.rootWeb) { rootWeb.Fields.AddFieldAsXml(fieldXml); } } Unfortunately, there is no such method for creating content type. If you want to use pre-defined GUID for your content type, you'll have create it using feature deployment.