Skip to main content

Posts

Showing posts from January, 2010

Renaming Type column in List

This question was posted in MSDN forum. How to rename Type column in a perticular List. In GUI, there is no option to rename the column. However, we can achieve this programmatically. Let's see the code: using (SPSite mySite = new SPSite("http://server/mySite/")) { using (SPWeb myWeb = mySite.OpenWeb()) { SPList myList = myWeb.Lists["ListName"]; SPField myField = myList.Fields["Type"]; myField.Title = "MyType"; myField.Update(); } } Here, in the perticular site and the list, get hold of "Type" field. Change the Title to whatever you want and call Update method. Now, refresh your List view! You can see the column name as "MyType"!

Adding Adobe PDF icon to Document Library

I was playing around with a document library. I uploaded a PDF file to the document library and I noticed that it displayed a white icon next to the PDF item instead of PDF icon which we see in our system. After digging more into this, here is how I got it resolved: Let's do step by step: Download the PDF icon from Adobe website (I chose 17x17 icon) Save the above GIF image into "12\TEMPLATE\IMAGES" folder Go to "12\TEMPLATE\XML" folder and open DOCICON.XML Under ByExtension tag, add a Mapping element for PDF GIF image. Key = "pdf" Value = "pdficon_small.gif" Save DOCICON.XML file Do IISRESET Refresh your page. Bingo! For all PDF files, it displays the correct PDF icon!

How to determine Site Definition programmatically

We can create different kind of sites such as Team Site, Document Workspace, Blog etc. Once we create such sites, how do we find out what kind of sites we have? I was trying to find this answer programmatically. Here is the simple code to get that. using (SPSite mySite = new SPSite("http://mysite")) { foreach (SPWeb myWeb in mySite.AllWebs) { Console.WriteLine("Name: " + myWeb.Title); Console.WriteLine("Template ID: " + myWeb.WebTemplateId.ToString()); Console.WriteLine("Template Name: " + myWeb.WebTemplate); Console.WriteLine("===================="); myWeb.Dispose(); } } In this code, I'm looping through each Web in the site collection and display the Title, Site Definition Template ID, and Site Definition template Name of each Web. Note that, it is a best practice to dispose SPWeb object explicitly when we are using SPWeb in foreach statement.

How to determine if the SPUser is a Manager!

This question was posted in MSDN forum. I did some research on this and got the logic to get it! First, how do we determine if the user is a manager? In AD, every manager will have Direct Reports. This is the assumption I've made to write the logic. Let's see the code: 1. First you should add a reference to Microsoft.Office.Server 2. Define the namespace: using Microsoft.Office.Server; using Microsoft.Office.Server.UserProfiles; 3. Add the below piece of code: using (SPSite mySite = new SPSite("http://mysite")) { ServerContext context = ServerContext.GetContext(mySite); UserProfileManager profileManager = new UserProfileManager(context); using (SPWeb myWeb = mySite.OpenWeb()) { foreach (SPUser user in myWeb.Users) { UserProfile profile = profileManager.GetUserProfile(user.LoginName); if (profile.GetDirectReports().Length > 0) //Manager! else // Not a manager } } } Her

Accessing User Profile details programmatically

When you configure User Profiles, you can get a lot of information of the SPUser. These details can also be fetched programmatically. Let me show you how to do that. Before we get into the coding, let me first explain you about user profile properties. To check what kind of user profile properties you have in your SSP, open your SSP -> User profile and Properties -> View Profile Properties. Here it displays the list of Profile Properties. You can edit some of the properties and set some conditions like whether user should be able to edit the property or not and so on. Let's look at the code: 1. First you should add a reference to Microsoft.Office.Server 2. Define the namespace: using Microsoft.Office.Server; using Microsoft.Office.Server.UserProfiles; 3. The actual code looks like this: using (SPSite mySite = new SPSite("my Site URL")) { ServerContext context = ServerContext.GetContext(mySite); UserProfileManager profileManager = new UserProfileManager(context