Skip to main content

Posts

Showing posts from 2009

HOW TO CREATE SPFieldLookup programmatically

This is a nice snippet I saw and will like to share Let us assume we have two list aList and bList already created. Requirement is to create a lookup column in aList which will look up values in bList. SPList aList = web.Lists["aList"]; SPList bList = web.Lists["bList"]; aList.Fields.AddLookup("Lookup", bList.ID, false); SPFieldLookup fldLookup = aList.Fields["Lookup"] as SPFieldLookup; fldLookup.LookupField = bList.Fields[SPBuiltInFieldId.Title].InternalName; fldLookup.Update();

ERROR DISPLAY SHAREPOINT

By default, the SharePoint web config only displays vague custom error which for a developer usually does not provide any meaning. To enable clear error display, do the following: Open the web config for editing, this is located inside the wwwroot. Make sure you select the right port number. The default post is 80. Find the "CallStack" attribute in the SafeMode section, change the value from false to true. Identify the "CustomErrors" in System.Web section, change the value from On to Off. Please note that this would be unnecessary in a production environment and it is only suitable in a test environment.

ERROR LOG IN SHAREPOINT

Nice little snippet to log errors using the object model and mininal code: try { } catch (Exception ex) { Microsoft.Office.Server.Diagnostics.PortalLog.LogString("Exception occurred while loading form: {0} || {1}", ex.Message, ex.StackTrace); } Merry Christmas

Customizing Alert Notifications and Alert Templates in WSS 3.0

Alerts are an e-mail subscription notification service in WSS 3.0. Users can create alerts to be notified of changes to list items (item level alert), documents, lists (list level alert), or document libraries. Alerts in Windows SharePoint Services 3.0 are quite flexible; when you create an alert you have options such as when to be notified and what kind of changes will trigger the alert. Alerts can be managed by users and administrators and customized by developers. After an alert has been created, changes that fit the criteria specified in the alert are recorded in an event log. Alerts are generated by a timer job that reads the event log, formats the alert email using an alert template, and then sends that email out to the user. Alert templates are stored in Alerttemplates.xml. Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS Alerttemplates.xml defines the format, contents, and properties used to create alert messages from each list type in Windo...

UNHIDE SHAREPOINT FIELD

I am writing this as a reminder for myself and for the sake of those that might need it. You come across fields in SharePoint that only shows while using CAML query builder but wouldn't show in the UI. These fields are system generated as part of the content type or library. To show them properly, you need to change a property called cantogglehidden which by default is readonly. SPSite site = new SPSite("http://moss01"); SPWeb web = site.RootWeb; web.AllowUnsafeUpdates = true; SPField spfield = web.Lists["Accounts"].Fields["Document Type"]; Type type = spfield.GetType(); MethodInfo miinfo = type.GetMethod("SetFieldBoolValue", BindingFlags.NonPublic | BindingFlags.Instance); miinfo.Invoke(spfield, new object[] { "CanToggleHidden", true }); spfield.Hidden = false; spfield.Update(); QED

SPDisposeCheck

Have you checked your assemblies before final deployment. It is the best practice to make sure that your code is safe for deployment with no memory leak. A new tool called SPDisposeCheck is now available to help you in checking and ensures you build good code. Without disposing SharePoint objects, you are subject to memory problems on the deployment server. This tool however recursively checks through your code and reports. You can check it out here. The SPDisposeCheck tool can be downloaded here .