Sharepoint solutions

April 29, 2009

Custom computed field in the display list form

Filed under: main — admin @ 4:02 pm

Sharepoint  provide two field type that can calculate value upon some formula to show information in the display list form – calculated field type and computed field type. Calculated field store its value in the database and update this value  only when list item has been updated. So, this field type can not be used to show data that can be changed independently of the list item. Computed field can only calculate formula that conists only other fields from list item. We can not derive custom field from spfieldcomputed and can only use caml to make this formula in the scheme.
  My task was – show in the list display form (dispform.aspx usually, but actually this name is set in the list scheme) some data that depends of the list item  and do not depends of it (in the list item1 there was referrence field refid to id of another item2 and I must show  transformed upon some fomula data from item2 – its name to uppercase for example).

Brief description of the solution:

To resolve this task I create new custom field derived from SPFieldText and overwrite
RenderFieldForDisplay method of control class that render this field.

Details description of the solution:

1. Make new visual studio project of type class library and create new class:

     // example of creating a custom field type
    public class SomeCustomField : SPFieldText
    {

        public SomeCustomField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName) { }

        public SomeCustomField(SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName) { }

        public override Microsoft.SharePoint.WebControls.BaseFieldControl FieldRenderingControl
        {
            get
            {
               
                BaseFieldControl control = new SomeCustomFieldControl();
                control.FieldName = this.InternalName;
                return control;
            }
        }
    }

    // custom field type uses helper class to initialize and render control
    public class SomeCustomFieldControl : BaseFieldControl
    {
            
        protected override string DefaultTemplateName
        {
            get
            {
                return @”SomeCustomFieldControl”;
            }
        }
       
        protected override void RenderFieldForDisplay(HtmlTextWriter output)
        {

            int IdOfItem2 = (int)this.ListItem["refid"];
            SPListItem item2 = this.ListItem.ParentList.GetItemById(IdOfItem2);
            string data = item2.Name.ToUpper();
            output.Write(data);
            base.RenderFieldForDisplay(output);
        }
  

    }
2.Compile this class library, make assembly, give it strong name and install it to GAC.
(How making all things are easy in sharepoint! ;) )

3.Make SomeCustomField.ascx with the following content:
<SharePoint:RenderingTemplate ID=”SomeCustomFieldControl” runat=”server”>
  <Template>
 
  </Template>
</SharePoint:RenderingTemplate>

Copy this file to the Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES directory
3.Make fldtypes_SomeCustomField.xml with the following content:
<?xml version=”1.0″ encoding=”utf-8″ ?>
<FieldTypes>
 

  <FieldType>
    <Field Name=”TypeName”>SomeCustomField</Field>
    <Field Name=”ParentType”>Text</Field>
    <Field Name=”TypeDisplayName”>SomeCustomField Status</Field>
    <Field Name=”TypeShortDescription”>SomeCustomField Status Field</Field>
    <Field Name=”UserCreatable”>TRUE</Field>
    <Field Name=”ShowInListCreate”>TRUE</Field>
    <Field Name=”ShowInSurveyCreate”>TRUE</Field>
    <Field Name=”ShowInDocumentLibraryCreate”>TRUE</Field>
    <Field Name=”ShowInColumnTemplateCreate”>TRUE</Field>
    <Field Name=”FieldTypeClass”>SomeCustomFieldAssembly.SomeCustomField, SomeCustomField, Version=1.0.0.0, Culture=neutral, PublicKeyToken=11ae1111111ddd11</Field>
    <RenderPattern Name=”DisplayPattern”>
      <Switch>
        <Expr>
          <Column/>
        </Expr>
        <Case Value=”">
        </Case>
        <Default>
        
          <Column SubColumnNumber=”0″ HTMLEncode=”TRUE”/>
        
        </Default>
      </Switch>
    </RenderPattern>
  </FieldType>
 
 
</FieldTypes>

Copy this file to the Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\XML

4. Install and activate feature.xml:

<?xml version=”1.0″ encoding=”utf-8″ ?>
<!– _lcid=”1033″ _version=”12.0.4017″ _dal=”1″ –>
<!– _LocalBinding –>
<Feature  Id=”11111111-1111-1111-85B9-5ED1D39C073B”
  Title=”CustomField”
  Description=”SomeCustomField Type”
  Version=”12.0.0.0″
  Scope=”Site”
  xmlns=”http://schemas.microsoft.com/sharepoint/” >
  <ElementManifests>
    <ElementManifest Location=”elements.xml”/>
  </ElementManifests>
</Feature>

where the elements.xml is:

 <Field ID=”{CA1A1D11-2123-4db7-B11A-1C0D05D01111}”
            Name=”SomeCustomFieldDerived”
            Group=”Custom”
           Type=”SomeCustomField”
           DisplayName=”SomeCustomField Derived”
           ShowInDisplayForm=”TRUE”
            Sortable=”FALSE”
            ReadOnly=”TRUE”
            Required=”false”    
            Hidden=”false”
      SourceID=”http://schemas.microsoft.com/sharepoint/v3
      StaticName=”SomeCustomFieldDerived”
     
      />
 
 That’s all now we can add this field type to our content type or list.

We can not make caml query to search this field and the real value of this field is null, so in the allitems.aspx listing the value will be also null.

March 21, 2009

Debug net code that is called asynchronously for the user in sharepoint

Filed under: main — admin @ 10:52 pm

There is some trouble to debug net code that  is called by the system in sharepoint. For example owstimer can call custom expiration formula class asynchronously for the user. So, the ordinary method to attach visual studio debugger do not help. The solution is very simple – insert Debug.Assert(1==2, “some message”) statement in your code. When the exeption occures, press retry and choose visual studio debugger. Cetainly the custom class must contain debug information and do not be optimized.

February 7, 2009

Can not delete forms folder in sharepoint

Filed under: main — Tags: , — admin @ 2:30 pm

There is a problem in wss 3.0 with sp1 – forms folder in document library can not be deleted or removed no  matter if it is first level folder or subfolder for document library. This problem resolved in Update for Windows SharePoint Services 3.0 (KB941422), (the name of file is Wss-kb941422-fullfile-x86-glb.exe).

URL to download:http://www.microsoft.com/downloads/details.aspx?FamilyID=78549f3c-3cd2-445e-9dc0-417ca5a4a079&displaylang=en

June 26, 2008

Dynamically creating report with report viewer

Filed under: main — admin @ 3:53 pm

Instead of using custom rdlc files for each report we can use LoadReportDefinition method of report viewer control and pass to it genarated report definition file. Download the example of crating rdlc file on the fly. To use this code copy both files into your web site, and open RMReport.aspx file in the browser. You can specify number of report columns by type it in the url: RMReport.aspx?col=4. See the using report viewer control in sharepoint for more information of using report viewer control in sharepoint.

Good luck.

May 13, 2008

Transactions in Sharepoint 2007

Filed under: main — admin @ 12:59 am

During the development the application in  sharepoint we made some operations  with the list items that must be proccessed as “all-or-nothing”,  such moving the item in the list and then change it field, or update multiple items simultaneously. Object model of the Sharepoint services does not contain methods that deal with transaction. So, I begin my researches and find some forum post http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2873895&SiteID=1. Moderator refers to
http://support.microsoft.com/kb/883316 and said that Sharepoint object model does not support transaction. I have read early some other posts that suggested to use Service Without Components http://weblogs.asp.net/paolopia/archive/2005/01/30/363312.aspx or http://msmvps.com/blogs/anguslogan/archive/2005/07/19/58191.aspx

and also to use new TransactionScope class http://www.eggheadcafe.com/software/aspnet/31533974/sharepoint-om–transacti.aspx. I try to use  Service Without Components or TransactionScope class but they did not work for me. So, the reference to Microsoft KB and argument that distributed transaction are based on com+ technology seems to be convincing and it is no way to use transaction simply in Sharepoint OM.

 But moderators that object to use transation in OM suggest to use WF in Sharepoint instead. It seems to me strange and I continue my reasearches.  I found interesting post http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1993200&SiteID=1 that refers to http://msdn2.microsoft.com/en-us/library/ms442249.aspx.  msdn said that Windows SharePoint Services-specific workflow activities are  batched and not committed until a commit point is reached but code activities are committed as they occur. But most of the SharePoint Services-specific workflow activities can not be  developed with Visual Studio,  only With Sarepoint Designer. So, I search again and  find another interested article http://blogs.msdn.com/sharepointdesigner/archive/2007/07/06/porting-sharepoint-designer-workflows-to-visual-studio.aspx  that teaches to use SD workflow in VS.

November 17, 2007

Using ReportViewer control in Sharepoint

Filed under: main — admin @ 6:31 pm

ReportViewer can be used in Sharepoint to make reports. To do this – place ReportViewer on the aspx page. Create rdlc report file to show some table data, for example table – “Name – Age”. Then link ReportPath property with rdlc file and add data source in the DataSources collection. As we fill these properties in code, then that example can be easy modified to work with different rdlc files and different table data. More over, we can use session to pass data source to the code from another page.

ReportViewer folder contains VS2005 project that demonstrate using of this control. The files MyReport.aspx and MyReport.aspx.cs must be copied to the “layouts” folder, the Report.rdlc file must be copied to the root of your application virtual directory, where web.config is. To run example – open in the browser your site URL, followed by the _layouts/Myreports.aspx. You will see the followed picture:

If the user do not have access to read from the root of your virtual directory an error will appear. To resolve this problem you must give users privilege to read rdlc files.

November 13, 2007

Session state in MOSS

Filed under: main — admin @ 4:38 pm

By default sesion state is enabled for farms in sharepoint. To prove that go to Central Administration ->Application Management ->Office SharePoint Server Shared Services -> Configure session state. But, to use session in your code you must turn on session either in web.config or in your pages. One line in web.config must look like:
<pages enableSessionState=”true” … and one line must be uncomment: <add name=”Session” type=”System.Web.SessionState.SessionStateModule”/>. To turn on session in the specific page – simply set EnableSessionState=”True” in the Page directive.

Powered by WordPress