Showing posts with label User Controls. Show all posts
Showing posts with label User Controls. Show all posts

Tuesday, June 24, 2008

SPList fields - parsing source-code

Here is the source-code to obtain an html table of fields for any list:

(Please note that this example takes place inside the Render(HtmlTextWriter writer) method:

using (SPSite site = new SPSite(SPContext.Current.Site.Url))

{

using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ServerRelativeUrl))

{

SPList list = web.Lists["YourList"];

SPListItemCollection iCol = list.Items;


writer.Write("<table border=\"1\">");

writer.Write("<tr><td><strong>Field name</strong></td><td><strong>Sample value (if any)</strong></td></tr>");

for (int i = 0; i < iCol[0].Fields.Count; i++)

{

writer.Write("<tr><td><strong>");

writer.Write(iCol[0].Fields[i].Title);

writer.Write("</strong><br/>" + iCol[0].Fields[i].Description);

writer.Write("</td><td>");

writer.Write(iCol[0].GetFormattedValue(iCol[0].Fields[i].Title));

writer.Write("</td></tr>");

}

writer.Write("</table>");

}

}


SPList fields

If you've ever worked with fields from a coding point of view in sharepoint 2007 or plan to do so, then this list is invaluable to you.

It contains all the default fields for a SPList item. (Remember that pages are essentially list items and the pages collection is essentially a list).

Field name Sample value (if any)
Content Type ID
0x0100E65E7B14B0BC434CB9F70A29DDD8C22E
Approver Comments

DocumentTitle
at work2.jpg
DocumentTitle
at work2.jpg
File Type

ID
3
Content Type
Item
Modified
6/24/2008 5:07 PM
Created
2/20/2008 1:41 AM
Created By
System Account
Modified By
System Account
Has Copy Destinations

Copy Source

owshiddenversion
2
Workflow Version
1
UI Version
512
Version
1.0
Attachments
False
Approval Status
Approved
Edit

Select
3
Instance ID

Order
300
GUID
{B16BDAD3-7279-4C90-890E-D64D4FF9FE6E}
Workflow Instance ID

URL Path
/corporate/divisions/officeoftheceo/capacitydepoloyment/agencies/Lists/BusinessPortalUploadRecord/3_.000
Path
corporate/divisions/officeoftheceo/capacitydepoloyment/agencies/Lists/BusinessPortalUploadRecord
Modified
6/24/2008 5:07 PM
Created
2/20/2008 1:41 AM
Item Type
0
Effective Permissions Mask
0x7fffffffffffffff
Name
3_.000
Unique Id
389a8e63-0d5d-42c8-b8dc-a3702f566496
ProgId

ScopeId
{8FF6C25F-7D37-4569-823F-4E1C2433F5CF}
HTML File Type

Edit Menu Table Start
3_.000
Edit Menu Table End
3
Name
3_.000
Name
3_.000
Type

Server Relative URL
/corporate/divisions/officeoftheceo/capacitydepoloyment/agencies/Lists/BusinessPortalUploadRecord/3_.000
Encoded Absolute URL
http://dvbmdas15:101/corporate/divisions/officeoftheceo/capacitydepoloyment/agencies/Lists/BusinessPortalUploadRecord/3_.000
File Name
3_
Property Bag

Level
1
Is Current Version
Yes


See next post for source-code on how to replicate this table.

Thursday, July 26, 2007

Web User Controls in Sharepoint

As a Sharepoint 2007 / WSS developer you sometimes need to achieve some sort of design constrained functionality which just cannot be solved by using web parts or anything else but user controls.

User controls are perfect because you can put them anywhere and not have to worry about sharepoint server controls and the like.

Let me guide you through the process of creating a simple "hello world" user control and then implementing it into your sharepoint site's home page.

1. Create a web project
For the sake of convenience, i strongly suggest that you create your web project in the following directory:
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\WebSite1\
(You can replace "WebSite1" with whatever you prefer to call the folder)

The reason for this is that you're gonna have to copy and paste your user controls from this directory to the CONTROLTEMPLATES directory at the following location:
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES



The two folders are geographically close, if you open up the layouts folder in windows explorer and enable your folder view, you will be able to see the CONTROLTEMPLATES folder a couple of folders up.
















2. Develop the User Control
I assume that you've created Web User Controls before and know what i'm talking about when i say "Develop the User Control".
I write all my code in C# and i use code-behind files.

I usually leave the Page_Load method blank and create a new one to the effect of
protected void RenderWhateverImDoing() ~ and put all my code in there. That way i have exact control of where in my user control the code gets executed because my .ascx file looks like this:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WhateverImDoing.ascx.cs" Inherits="WhateverImDoing" %>
<%RenderWhateverImDoing(); %>


So in your RenderWhateverImDoing() method, add the code: Response.Write("Hello Sharepoint World!"); or something similar and save all files.

3. Copy the User Control to the CONTROLTEMPLATES folder
After saving the files, go to the project folder in windows explorer and copy the WhateverImDoing.ascx file and the WhateverImDoing.ascx.cs files to the CONTROLTEMPLATES folder.

4. Register the control.
I assume you are familiar with the master page and layout page concepts and that you know how to create both and utilise them in the Sharepoint environment.

The beauty of this is that you can place your user control in either the master page or the layout page. Place it in the master page if you want your user-control to be available and active as part of the template or put it into a layout page if you wish to only make it available on one specific page. If you put it in a layout page you'll have to apply that layout to the page you want to affect in sharepoint via the "edit page settings" option in the "Actions" menu or via the "Manage content" section of sharepoint.

The control register page directive goes at the top of the page, just below all other page directives you already have. Note that page directives all start with "<%@".
Now go ahead and add the registration page directive for your new user control:
<%@ Register Src="~/_controltemplates/WhateverImDoing.ascx" TagName="WhateverImDoing" TagPrefix="MyControls" %>

5. Add the Server tag to the HTML
Now that you've registered the control, you need to reference it in the appropriate place by using it as a server control in HTML. Whatever HTML gets generated by your user control will replace your user control server tag at runtime. So in other words, wherever you put your Tag, thats where "Hello Sharepoint World!" will get inserted.

Your server tag will look like this:
<MyControls:WhateverImDoing id="WhateverImDoingControl" runat="server">


And thats it! There is one last step to this if you've added your user control to a page layout file and that is to go and apply the specified layout to the page you want to display the user control on. Other than that all you need to do is go and test by navigating to the page (any page that has the customised master page applied to it if you used it in a master page).

IMPORTANT NOTE:
Whenever you make changes to the code-behind of your user control, you need to "tap" the .ascx file. This entail modifying the .asxc page by either adding a space or making some other kind of negligible change to the file so that the modified date on the file changes. You need to do this in order to avoid caching problems in sharepoint. So when you've made code changes to your user control and the changes arent reflecting on the portal, it might just be that you forgot to "tap" the .ascx file and sharepoint is still showing you the old version.

-Fin