December 19, 2008

Using SharePoint rich text editor (InputFormTextBox) in your application pages

As the SharePoint rich text editor is located in the Microsoft.SharePoint.WebControls namespace (Microsoft.SharePoint assembly), you have to add the following Register tag on your aspx page.

<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
After that, you simply have to add the InputFormTextBoxt control in your page.

<SharePoint:InputFormTextBox Rows="10" MaxLength="1000" id="txtMyRichTextBox" runat="server" RichText="true" RichTextMode="FullHtml" TextMode="MultiLine"  Height="400px" Width="250px"></SharePoint:InputFormTextBox>
The result is the following:



MSDN link: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.inputformtextbox.aspx

December 16, 2008

Issue: SPQuery doesn't return items from sub-folders

In using SPQuery object to get specific items from a list, you will see that it returns only the items from the root folder. To get also items from sub-folders, you have to set ViewAttributes with "Scope='RecursiveAll'" like in the following piece of code.

using (SPSite mySiteCollection = new SPSite("http://myserver/myspsite"))
{
    using (SPWeb mySite = mySiteCollection.OpenWeb(mySiteCollection.RootWeb.ID))
    {
        SPList studentsList = mySite.Lists["Students"];
        SPQuery query = new SPQuery();
        query.ViewAttributes = "Scope='RecursiveAll'";
        SPListItemCollection items = studentsList.GetItems(query);
    }
}

December 13, 2008

SharePoint Used Space Information

SharePoint Used Space Information is a console application which allows to retrieve information about space used (bytes) by all sites and sub-sites for a specific site collection into .csv files.

This tool uses the StorageManagementInformation method of the SPSite class to retrieve the usage information.

You can find the solution on CodePlex: http://www.codeplex.com/SPUsedSpaceInfo

December 12, 2008

How to set custom forms (new, edit and display) for a specific content type by code

Let's imagine a content type e.g. Student and you want to set custom forms for it (new, edit and display) as in the previous example. The custom forms have to be set using the NewFormUrl, EditFormUrl and DisplayFormUrl properties of the SPContentType class.

using (SPSite mySiteCollection = new SPSite("http://myserver/myspsite"))
{
    using (SPWeb mySite = mySiteCollection.OpenWeb(mySiteCollection.RootWeb.ID))
    {
        SPList studentsList = mySite.Lists["Students"];
        SPContentType studentContentType = studentsList.ContentTypes["Student"];
        string newurl = "_layouts/STUDENT/APPLICATIONPAGES/NewStudent.aspx";
        string editurl = "_layouts/STUDENT/APPLICATIONPAGES/EditStudent.aspx";
        string displayurl = "_layouts/STUDENT/APPLICATIONPAGES/DisplayStudent.aspx";
        studentContentType.EditFormUrl = editurl;
        studentContentType.NewFormUrl = newurl;
        studentContentType.DisplayFormUrl = displayurl;
        studentContentType.XmlDocuments.Delete("http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url");
        studentContentType.Update();
        studentsList.Update();
    }
}

December 08, 2008

How to set custom forms (new, edit and display) for a specific content type by CAML definition

Let's imagine a content type e.g. Student and you want to set custom forms for it (new, edit and display). The custom forms have to be declared under the XmlDocuments node of the content type CAML definition.

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <ContentType
        ID="0x01004037ADCF3A5C4b648FFC4DA96C965CDE"
        Name="Student"
        Description="Create a new student"
        Version="0"
        Group="Custom Content Types" >
    <FieldRefs>
      <!-- add in and rename built-in WSS Title column-->
      <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" DisplayName="Name" Sealed="TRUE"  />
      <!-- custom fields-->
      <FieldRef ID="{756D3B72-AC72-4e0a-8B6D-1E2573251816}" Name="CardNumber" DisplayName="Card Number" />
      <FieldRef ID="{0D6C76FD-A9B6-4613-9429-06385411E9D7}" Name="StudyYear" DisplayName="Study Year" />
    </FieldRefs>
    <XmlDocuments>
      <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url">
        <FormUrls xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url">
          <New>_layouts/STUDENT/APPLICATIONPAGES/NewStudent.aspx</New>
          <Edit>_layouts/STUDENT/APPLICATIONPAGES/EditStudent.aspx</Edit>
          <Display>_layouts/STUDENT/APPLICATIONPAGES/DisplayStudent.aspx</Display>
        </FormUrls>
      </XmlDocument>
    </XmlDocuments>
  </ContentType>
</Elements>