Tuesday, March 8, 2011

SharePoint PictureLibrary Thumbnail Url Vs Picture Url

I had hard time to get the complete Url of the picture as well thumbnail directly (using any property) inserad of managing with server relative/absolute url, thumbanil folder /t and the picture name.

But we have full control through SPObject Model(SPBuiltInField class) to get the picture/thumbnail Url directly.
SPList spList = web.Lists["ImagesLibrary"];
SPListItem item = spList.Items.GetItemById(itemID);

//Thumbnail Url
string thumbnailUrl = item[SPBuiltInFieldId.EncodedAbsThumbnailUrl].ToString();

//Picture Url
string pictureUrl = item[SPBuiltInFieldId.EncodedAbsUrl].ToString();
The above Urls are the full Urls of the thumbnail and picture respectively.

To get the absolute, relative Urls of the picture the Url can be managed as below.
Uri uri = new Uri(thumbnailUrl);
string relativePath = uri.AbsolutePath;

Thursday, February 24, 2011

Webpart to render New Form dynamically for a list item

To generate/render a new form in a webpart dynamically to create/add a list item. Where the field name, type of each field will be fetched and rendered respectively. It holds the full control on all the properties of a column.
//Create the table object that we are going to add the rows and cells to for our data entry form
Table tbl = new Table();
tbl.CellPadding = 0;
tbl.CellSpacing = 0;

// Get the site that this web part is running on.
SPWeb spWeb = SPContext.Current.Web;

// Get the list we are going to work with
SPList spList = spWeb.Lists["MyList"];

// Loop through the fields
foreach (SPField spField in spList.Fields)
{
// See if this field is not hidden
if (!spField.Hidden && !spField.ReadOnlyField && spField.Type != SPFieldType.Attachments)
{
// Create the label field
FieldLabel fieldLabel = new FieldLabel();
fieldLabel.ControlMode = SPControlMode.New;
fieldLabel.ListId = spList.ID;
fieldLabel.FieldName = spField.InternalName;

// Create the form field
FormField formField = new FormField();
formField.ControlMode = SPControlMode.New;
formField.ListId = spList.ID;
formField.FieldName = spField.InternalName;

// Add the table row
TableRow tblRow = new TableRow();
tbl.Rows.Add(tblRow);

// Add the cells
TableCell tblLabelCell = new TableCell();
tblRow.Cells.Add(tblLabelCell);
TableCell tblControlCell = new TableCell();
tblRow.Cells.Add(tblControlCell);

// Add the control to the table cells
tblLabelCell.Controls.Add(fieldLabel);
tblControlCell.Controls.Add(formField);

// Set the css class of the cell for the SharePoint styles
tblLabelCell.CssClass = "ms-formlabel";
tblControlCell.CssClass = "ms-formbody";
}
}

// Create the save button
SaveButton btnSave = new SaveButton();
btnSave.ControlMode = SPControlMode.New;
btnSave.ListId = spList.ID;

// Create the row for the save button
TableRow tblButtonRow = new TableRow();
// Create the cell for the save button
TableCell tblButtonCell = new TableCell();

tblButtonCell.ColumnSpan = 2;
tblButtonRow.Cells.Add(tblButtonCell);
tbl.Rows.Add(tblButtonRow);

// Add the table to the web part controls collection
this.Controls.Add(tbl);

Monday, November 8, 2010

SPQuery on SPView gives an exception "Value does not fall within the expected range"

Inorder to get the results from the specific view of a list where the view is created with some filter conditions SPQuery should be run on spcific view (SPView) of a list instead default list.
SPList spList = spWeb.Lists["List Name"];
SPView view = spList.Views["View Name"];
SPQuery query = new SPQuery(view);
SPListItemCollection collection = spList.GetItems(query);
The statement executes and gets the result collection, but the result item(listitem) of collection doesn't have the properties or columns loaded by default.

So collection[0]["Title"] gives "value does not fall within the range" exception.

To avoid the exception, all the fields which are going to be read in the further statements should be specified for the view as viewfields.
query.ViewFields = "
Similarly 'n' number of fields which are going to be used in further can be specified as view fields.

Friday, July 16, 2010

SharePoint overriding the Core.css styling for specific site

The standard default core.css file is located in …\12\TEMPLATE\LAYOUTS\1033\STYLES. This STYLES folder is a virtually mapped via the /_layouts virtual directory that is mapped to each and every site in SharePoint.  So, the core.css file is accessible to every site by simply using the URL of “/_layouts/1033/styles/core.css”.  For every page that SharePoint renders, it renders a link to the core.css using this path. 

However, for any site that has been configured to use a custom core.css style sheet via the CustomizeCss method on the SPWeb class, the URL rendered in each page references a core.css file located in a different location.  This is accomplished in C# by the following code where web is an instance of the SPWeb class for the web site of your choice.

web.CustomizeCss(“core.css”);
web.Update();

The CustomizeCss(“core.css”) statement tells SharePoint to use a custom copy of the SharePoint core.css file.  Once this CustomizeCss method is executed, a copy of the core.css file found in the …\12\TEMPLATE\LAYOUTS\1033\STYLES folder is copied to the folder (access this by Sharepoint Designer) named "_styles" on the applied site.  

Modifying this core.css file will affect only the current site. 

Reverting this site back to using the original global (un-customized) copy of the core.css file can be accomplished just a easy using the following code.

web.RevertCss(“core.css”);
web.Update();

Executing the RevertCss method does not remove the custom core.css file on the web site’s “/_styles” folder.  It simply tells the site to no longer use it and to use the original core.css found in the C:\…\12\TEMPLATE\LAYOUTS\1033\STYLES folder; which is the same folder as the as the “/_layouts/1033/styles” folder.

So we now know how to use the CustomizeCss and the RevertCss methods on the SPWeb class to provide an easy way to point the site to a new custom style sheet file.

SharePoint libraries Types and Values

Following is a description for the available types (available in the WSS SDK)

Value Description
100 Generic list
101 Document library
102 Survey
103 Links list
104 Announcements list
105 Contacts list
106 Events list
107 Tasks list
108 Discussion board
109 Picture library
110 Data sources
111 Site template gallery
113 Web Part gallery
114 List template gallery
115 XML Form library
120 Custom grid for a list
200 Meeting Series list
201 Meeting Agenda list
202 Meeting Attendees list
204 Meeting Decisions list
207 Meeting Objectives list
210 Meeting text box
211 Meeting Things To Bring list
212 Meeting Workspace Pages list
300 Portal Sites list.
1100 Issue tracking
2002 Personal document library
2003 Private document library

BaseType

0 — Custom List
1 — Document Library
2 — Not used
3 — Discussion Forum
4 — Surveys
5 — Issues List

so, if you are developing custom picture library set Type="109" and BaseType="1" (because picture library mainly based on document library)