Saturday, December 12, 2009

Uploading File to Document Library Using ASP.Net

1. Add reference to Microsoft.Sharepoint.Dll
2. [Code Behind ]

using (SPSite site = new SPSite("YourSite"))
{
using (SPWeb web = site.OpenWeb())
{
SPFolder folder = web.GetFolder(YourDocumentLibrary);
SPFileCollection files = folder.Files;
FileStream fStream = File.OpenRead(OfflineUploadCtrl.PostedFile.FileName);
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
Hashtable MetaDataTable = new Hashtable();
MetaDataTable.Add("Comments", "MyFirstUpload");
SPFile currentFile = files.Add(YourDocLibraryPath + OfflineUploadCtrl.FileName, contents, MetaDataTable, true);
}
}

3. UI Code
<asp:FileUpload ID="OfflineUploadCtrl" runat="server" />
<asp:Button ID="UploadButton" runat="server" Text="Upload"
onclick="UploadButton_Click" />

Steps to create and deploy a custom web part using Asp.Net in Office Server 2007

Introduction

Office Server 2007 provides lot of out-of-box web parts to make development very easy to most of the scenarios. Still we may need to do some tailor-made solutions to meet our customer demands. We are going to see here how to create a custom web part and deploy the web part in our site and use it. Since the task of creation and deployment is important more than the sample it is up to the reader to put his own thoughts to that.
Web Part Creation

1. Create a web application
2. Add a user control to the web application
3. Add reference to Microsoft.Sharepoint.dll to the web application
4. Add a class file and do as mentioned below
[Code]
using System;
using System.Web.UI;

namespace YourNameSpace
{
public class YourClassName:Microsoft.SharePoint.WebPartPages.WebPart
{
#region "Global variable declarations"

protected Control _control = null;
protected string _exception = String.Empty;

//This UserControls is a folder in the VirtualServer
//directory of your Sharepoint WebApplication.
//We have to create this folder in the VirtualServer
//directory and copy the UserControl at that location
protected string UserControlPath = @"~/UserControls/";
protected string UserControlFileName = @"YourUserControl.ascx";

#endregion

#region "Create Child Controls method to create the controls for the webpart"

protected override void CreateChildControls()
{
try
{
_control = this.Page.LoadControl(UserControlPath + UserControlFileName);
this.Controls.Add(_control);

}
catch (Exception ex)
{
_exception += "CREATE ERROR:" + ex.Message;
}
finally
{
base.CreateChildControls();
}
}
#endregion

#region "RenderControls method , to render the controls inside the webpart class"

protected override void RenderContents(HtmlTextWriter writer)
{
try
{
base.RenderContents(writer);
}
catch (Exception ex)
{
_exception += "RENDER ERROR:" + ex.Message;
}
}

#endregion
}
}

[/Code]

5. Go to Solution Explorer -> Properties - > Assembly and add the following line as last
[assembly:System.Security.AllowPartiallyTrustedCallers]
6. Create an xml file with the name Manifest and add the following stuff

xmlns="http://schemas.microsoft.com/sharepoint/">










Change the SolutionId with a new GUID
7. Add a new file to the application with the name somename.ddf and add the following conent
;*** SharePoint Features Example MakeCAB Directive file
;
.OPTION EXPLICIT ; Generate errors
.Set CabinetNameTemplate=YourWSP.wsp
.set DiskDirectoryTemplate=CDROM ; All cabinets go in a single directory
.Set CompressionType=MSZIP ;** All files are compressed in cabinet files
.Set UniqueFiles="OFF"
.Set Cabinet=on
.Set DiskDirectory1=WSPFolder
;
; ** CAB Root
Manifest.xml
bin\YourWebApp.dll
;
;*** End

8. Add makecab.exe to the application
9. Open the command Prompt and execute the following
Makecab.exe –f Yourname.ddf
Web Part Deployment

10. For convenience open one more command prompt and execute the following
Stsadm –o addsolution filename [Your WSP Path ]\YourName.wsp
Changedirectory to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN
11. Go to Central Admin - > Operation - > Solution Management and follow the steps





Using the Web Part in the Site

12. Go to the site where you have deployed the webpart. Click SiteActions -> Modify all settings
13. Click Webparts [ in the gallery section]
14. In the webpart gallery -> select new -> select your webpart and click populate gallery.
15. Go to the page where you want to add the webpart.
16. Click SiteActions / Edit Page
17. Click the Add Webpart - > find your webpart in Mics zone and add.
There are two more things we need to do :
1. Go the site virtual directory and open the web.config file
And change the trust level from minimal to full
2. Create a new folder in the site virtual directory with the name what you have provided in the Step 4 and add your ascx file to this folder.

That’s it . You are good to go with your new web part

Friday, October 30, 2009

Optional Last field in BizTalk Schema

Hi,

There may be situations where in the last field in your schema may or may not be available. When we generate a schema for this kind some times it will show "Unexpected token found....."

To avoid the same we can try this.

1. Go to your schema
2. Select the last field which may be optional
3. Set min occurs = 0

HTH

Thursday, October 8, 2009

Generate Sequence number using Mapper

To generate a sequence number of a particular record of input shema in result schema we can use either xslt or a functoid in mapper.

This is very simple

1. Drag and Drop Iteration functoid to the mapper.
2. Connect the record header to the functoid as input
3. Connect the result schema sequence number element to the functoid.

That's it

MJ

Thursday, August 27, 2009

The application domain or application pool is currently running version 4.0 or later of the .NET Framework. This can occur if IIS settings have been set to 4.0 or later for this Web application

Error :

 

“The application domain or application pool is currently running version 4.0 or later of the .NET Framework. This can occur if IIS settings have been set to 4.0 or later for this Web application, or if you are using version 4.0 or later of the ASP.NET Web Development Server. The <compilation> element in the Web.config file for this Web application does not contain the required 'targetFrameworkMoniker' attribute for this version of the .NET Framework (for example, '<compilation targetFrameworkMoniker=".NETFramework,Version=v4.0">'). Update the Web.config file with this attribute, or configure the Web application to use a different version of the .NET Framework.”

 

 

Solution :

 

Go to the web.config and set the targetframeworkmoniker as below

 

<compilation debug="true"   targetFrameworkMoniker=".NETFramework,Version=v4.0" />

 

Hope this helps

 

 

A name was started with an invalid character - WCF 4.0

The below error may lead the developer to confusion.

A name was started with an invalid character. Error processing resource 'http://localhost:81/MyWCFHello/MyHelloWcfApp.Servi...
<%@ ServiceHost Service="MyHelloWcfApp.Service1" %>
-^


Actually problem lies in your IISAdmin.

Follow the steps to resolve the same.
1. Open Inetmgr
2. Select your website
3. right click to properties -> Select ASP.Net tab
4. Change the version to ASP.Net 4.0

Hope this helps

Tuesday, August 25, 2009

WCF 4.0 Beta - Service Configuration Enhancements

 

Introduction

 

The .Net Framework 4.0 beta1 has shown lots of improvements in the area of Windows Communication Foundation. The new version has simplified the developer experience. It also provides us a rich integration with Workflow foundation. On top of all the improvements done one thing stands apart.

That is nothing but easier configuration of your services.

 

List of Configuration Improvements

1.      File less activation

2.      Default Endpoint

3.      Default Protocol Mapping

4.      Default Binding Configurations

5.      Default Behavior Configurations

 

File – less  Activation

 

In .Net 3.5 we were using .svc files to expose the services. We can configure the .svc extension by using IIS filter with little complexity.  Now .Net 4.0 introduces file-less activation for the services. What they mean by this is “There is no more .svc files”.

Following configuration will set you free from .svc files.

<configuration>

  <system.serviceModel>

    <serviceHostingEnvironment>

      <serviceActivations>

        <add relativeAddress="/MyPath" service="MyService"/>

      </serviceActivations>

    </serviceHostingEnvironment>

  </system.serviceModel>

</configuration>

 

Now we can browse the site like http://localhost/MyIISSite/MyPath.

 

Isn’t it cool ?

 

Default Endpoint

 

In WCF 3.5 / 3.0 if we host WCF service without endpoints it will throw error. Isn’t it ?

In .net 4.0 it is no longer an issue. In the runtime, default endpoints will be added

When the open method of ServiceHost is called, it will check the configuration file for the number of endpoints. If it is zero it will call the public method AddDefaultEndPoints to add the endpoint. One endpoint will be added per base address per service.

 

Default Protocol Mapping

 

WCF 4.0 allows you to map the transport protocol schemes and the WCF bindings. There are two ways to map the same. One in machine.config and the other by overriding  application configuration file.

In Machine.Config

    <protocolMapping>

        <add scheme="http" binding="basicHttpBinding"/>

        <add scheme="net.tcp" binding="netTcpBinding"/>

        <add scheme="net.pipe" binding="netNamedPipeBinding"/>

        <add scheme="net.msmq" binding="netMsmqBinding"/>

    </protocolMapping>

 

In App.Config

<system.serviceModel>

    <protocolMapping>

      <add scheme="http" binding="wsHttpBinding"

           bindingConfiguration="secureConfiguration"/>

    </protocolMapping>

  </system.serviceModel>

 

This will be useful if  in my application I am always going to use wshttpbinding rather than basic httpbinding for http protocol.

 

 

Default Binding Configurations

 

In WCF 3.5/3.0 we will define a named binding configuration which will be used against any endpoints. The problem here is we need to specify the configuration properly in the endpoints which may be cumbersome when we have more endpoints.

In WCF 4.0 we no longer have the name attribute. This will be considered as default and used by all default endpoints

<configuration>

  <system.serviceModel>

    <bindings>

      <basicHttpBinding>

        <binding messageEncoding="Mtom"> <!—No Name attribute -->

        </binding>

      </basicHttpBinding>

    </bindings>

  </system.serviceModel>

</configuration>

 

Default Behavior Configurations

 

Similar to Binding Configurations , WCF 4.0 supports default behavior Configurations.

<configuration>

  <system.serviceModel>

    <behaviors>

      <serviceBehaviors>

        <behavior> <!-- notice no name attribute -->

          <serviceMetadata httpGetEnabled="true"/>

        </behavior>

      </serviceBehaviors>

    </behaviors>

  </system.serviceModel>

</configuration>

 

Conclusion

 

WCF 4.0 provides more features apart from configuration enhancements which will reduce the developer burden as well as provide more opportunities to use WCF in new environments.

The configuration changes made in WCF 4.0 is for sure to make a difference to the developer / IT department world as they are the more concerned users.