Thursday, August 19, 2010

How to get a month name from date

We can get the month name using following statement

CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(datValue.Month)

Cheers..

How to get all available page layouts which can be used to create and page

There was a task in which i had to check what all page layouts are available for a current site which can be used to create a new page. So here is the solution

PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);

PageLayout[] pageLayouts = pubWeb.GetAvailablePageLayouts();

PageLayout pageLayout = pageLayouts.Where(p => p.Title.Trim() == item["LayoutName"].ToString().Trim()).FirstOrDefault();

if (pageLayout == null)
.......

Cheers....

Check if List or document/Picture library already exists

During one of my project i came accross this situation many time when i had to check whether list or document library already exists or not. So here is the solution


To check document/picture library there is an existing method which can be used like below

if (!web.GetFolder(strName).Exists)
{
......
}

Here 'strName' is the name of document/picture library.

But for list this method does not work. Unfortunately there is no method to check if list exists so we need to do a trick for that, check out below

SPList list = null;
try
{
list = web.Lists[strListTitle];
}
catch { }
if (list == null)
{
.....

Cheers.





Sunday, August 1, 2010

Check if Page is already checked out

It is very helpful while editing the contents of a publishing page to check if desired page is already checked or not.

Here is is the code to check this

Sol 1:

if (pPage.ListItem.File.CheckOutType == SPFile.SPCheckOutType.None)
pPage.CheckOut();

Sol 2:

SPFile fileSource = web.GetFile(strSourceFilePath);
SPUser user = null;
user = fileSource.CheckedOutBy;

if (user != null)
{....


If we does not check so then we may get error that page is already checked out.

Open a SharePoint popup after server trip

Last day i had a task in which when user clicks a button few pages got created at runtime and after this i had to show the popup window which will show a aspx page having status of all created pages whether successfully created or failed.

Challenge was to open the popup when button click server trip done (Page rerendered).

My method was creating a javascript at run time to open a popup. But issue was when it runs i gets a javascript error.

"Object Doesn't support this property or method"

Issue was my javascript was getting called even before its supporting js files gets load.

After some rnd i found the solution and here it is.

string strScript = "function OpenDialog() { var option = SP.UI.$create_DialogOptions(); option.url = '" + strPath + "'; option.width = 380; option.height = 330; SP.UI.ModalDialog.showModalDialog(option); } ExecuteOrDelayUntilScriptLoaded(OpenDialog, \"sp.ui.dialog.js\");";

ClientScript.RegisterClientScriptBlock(this.GetType(), "ShowStatusfunction", strScript, true);


Key thing is ExecuteOrDelayUntilScriptLoaded(OpenDialog, \"sp.ui.dialog.js\");";

What it does is, it will wait untill the specified js gets load then it will call the method

Cheers...





Tuesday, July 27, 2010

How to add a picker to select a site or website and pass the selected site url in textbox


check this article from PlanetWilson SharePoint Blog its good

SharePoint field objects – classname, name and types

While working on one of SharePoint project I searched for classnames and fieldtypes that I needed to identify all different field types.

Following table shows most important fields for list.

important fields.

Classname Type TypeAsString TypeDisplayName TypeShortDescription
Microsoft.SharePoint.SPFieldBoolean Boolean Boolean Yes/No Yes/No (check box)
Microsoft.SharePoint.SPFieldCalculated Calculated Calculated Calculated Calculated (calculation based on other columns)
Microsoft.SharePoint.SPFieldChoice Choice Choice Choice Choice (menu to choose from)
Microsoft.SharePoint.SPFieldComputed Computed Computed Computed Computed
Microsoft.SharePoint.SPFIeld ContentTypeId ContentTypeId Content Type Id Content Type Id
Microsoft.SharePoint.SPFieldCurrency Currency Currency Currency Currency ($, , ?)
Microsoft.SharePoint.SPFieldDateTime DateTime DateTime Date and Time Date and Time
Microsoft.SharePoint.SPFieldFile File File File File
Microsoft.SharePoint.SPField Guid Guid Guid Guid
Microsoft.SharePoint.SPFieldNumber Integer Integer Integer Integer
Microsoft.SharePoint.Portal.WebControls.BusinessDataField Invalid BusinessData Business data Business data
Microsoft.SharePoint.Publishing.Fields.ContentTypeIdFieldType Invalid ContentTypeIdFieldType Content Type Id Content Type Id
Microsoft.SharePoint.Publishing.Fields.HtmlField Invalid HTML Publishing HTML Full HTML content with formatting and constraints for publishing
Microsoft.SharePoint.Publishing.Fields.ImageField Invalid Image Publishing Image Image with formatting and constraints for publishing
Microsoft.SharePoint.Publishing.Fields.LayoutVariationsField Invalid LayoutVariationsField Variations Page Layout Variations
Microsoft.SharePoint.Publishing.Fields.LinkField Invalid Link Publishing Hyperlink Hyperlink with formatting and constraints for publishing
Microsoft.SharePoint.Publishing.Fields.PublishingScheduleEndDateField Invalid PublishingScheduleEndDateFieldType Publishing Schedule End Date Publishing Schedule End Date
Microsoft.SharePoint.Publishing.Fields.PublishingScheduleStartDateField Invalid PublishingScheduleStartDateFieldType Publishing Schedule Start Date Publishing Schedule Start Date
Microsoft.SharePoint.Publishing.Fields.SummaryLinkField Invalid SummaryLinks SummaryLinks Summary Links data
Microsoft.Office.Server.WebControls.FieldTypes.SPFieldTargetTo Invalid TargetTo Audience Targeting Audience Targeting
Microsoft.SharePoint.SPFieldLookup Lookup Lookup Lookup Lookup (information already on this site)
Microsoft.SharePoint.SPFieldLookup Lookup LookupMulti Lookup Lookup (information already on this site)
Microsoft.SharePoint.SPFieldNumber Number Number Number Number (1, 1.0, 100)
Microsoft.SharePoint.SPFieldRecurrence Recurrence Recurrence Recurrence Recurrence
Microsoft.SharePoint.SPFieldMultiLineText Note Note Multiple lines of text Multiple lines of text
Microsoft.SharePoint.SPFieldText Text Text Single line of text Single line of text
Microsoft.SharePoint.SPFieldUrl URL URL Hyperlink or Picture Hyperlink or Picture
Microsoft.SharePoint.SPFieldUser

Samples:

SPSite site = new SPSite("http://office2007");
    SPWeb web = site.OpenWeb();
    SPList list = web.Lists["Documenten"];
 
    // Add a new text field
    list.Fields.Add("NewTextField", SPFieldType.Text, false);
 
    // Add a new HTML field
    HtmlField htmlField = new HtmlField(
        list.Fields, "HTML", "NewHTMLField");
    list.Fields.Add(htmlField);