Sunday, February 1, 2009

Upload a document in DOCUMENT LIBRARY in WSS 3.0 programatically.

As I mentioned there are two ways to interact with WSS 3.0
1)WebService Approach
2)Object model approach.

So below is the code you have to do while following the service approach to upload a document programatically.


[WebMethod]
public bool UploadTemplate(Byte[] fileInBytes, string FileName, string MetaInformation)
{
try
{
NetworkCredential objCredential = new NetworkCredential();
objCredential.Domain = ConfigurationSettings.AppSettings["NetworkDomain"];
objCredential.UserName = ConfigurationSettings.AppSettings["NetworkUserName"];
objCredential.Password = ConfigurationSettings.AppSettings["NetworkPassword"];

string[] destURI = { ConfigWrapper.WebSiteUrl + ConfigWrapper.DoucmentDirectory + "/" + FileName };

Copy webCopyObj = new Copy();
webCopyObj.Credentials = objCredential;
webCopyObj.Url = ConfigWrapper.CopyServiceUrl;

FieldInformation myFieldInfo = new FieldInformation();
myFieldInfo.DisplayName = "MetaInformation";
myFieldInfo.Value = MetaInformation;
myFieldInfo.InternalName = "MetaInformation";
myFieldInfo.Type = FieldType.Text;
FieldInformation[] myFieldInfoArray = { myFieldInfo };

CopyResult myCopyResult2 = new CopyResult();
CopyResult[] myCopyResultArray = { myCopyResult2 };

CopyResult[] result;

uint documentId = webCopyObj.CopyIntoItems("http://null", destURI, myFieldInfoArray.ToArray(), fileInBytes, out result);
byte[] temp;
uint tempId = webCopyObj.GetItem(destURI[0], out myFieldInfoArray, out temp);
Guid myItemGUID = myFieldInfoArray[0].Id;
return true;
}
catch
{
return false;
}
}



webCopyObj will be the object of the copy web service taken from the site.So for example you have to include the web reference from http://localhost:15182/_vti_bin/Copy.asmx.which referes to /_vti_bin/Copy.asmx.


Will write about the second approach (Object model approach) later.


ConfigWrapper is the object of the class written for fetcing the information from appSetting section from web.config

Monday, January 5, 2009

Sharepoint Starting.

Hi,

I am new to sharepoint these days and I have seen some pepole who have heard about this sharpoint but never used its feature.

So, I decieded to share my knowledge upon share point with the Guys who are just starting this technology.

I came to know that we can access sharepoint programatically also.The good thing about sharepoint is it exposes 2 ways to interact with sharepoint site.

1. Sharepoint as a object model.
2. As Web Service Interaction.

Well hope you have heard of WSS 3.0. The full form of WSS is Windows Sharepoint Server. It exposes some web service for the developers who want to handle the site programatically and when you install sharepoint 2007 it also give some some more web services which a developer and use to manage the web site.

The second approach is the object model approach. In this approach developer has to include the dll and play with the web site as a object model (like namespaces, classes and Object etc.)

Hope this helps for the beginners.I will keep updating you about the sharepoint or WSS R&D, I will do.

Thanks

Monday, December 22, 2008

C# Program to display Prime Number

Program to display the PrimeNumber

public class PrimeNumber
{
public void DisplayPrimeNumberUpTO(string number)
{
int max = int.Parse(number);
List previousPrimes = new List();
previousPrimes.Add(2);
if (max < 2) return; // none
for (int i = 3; i <= max; i++)
{
int maxDivisor = (int)Math.Floor(Math.Sqrt(i));
bool foundDivisor = false;
for (int j = 0; j < previousPrimes.Count; j++)
{
if (previousPrimes[j] > maxDivisor)
break;
if ((i % previousPrimes[j]) == 0)
{
foundDivisor = true;
}
}
if (!foundDivisor)
{
previousPrimes.Add(i);
}
}
for (int i = 0; i < previousPrimes.Count; i++)
{
Console.WriteLine("Prime: {0}", previousPrimes);
}
}
}

Calculate factorial.

C# Program to calculate the factorial.

public class Factorial
{
public long FactorialCalculate(long val)
{
return val == 1 ? val : val * FactorialCalculate(val - 1);
}
}

A constructor can call another overloaded constructor

A constructor can call another overloaded constructer but there is a trick to make this call possible.

So in the following example when you create the instance of CallConstructor class without parameter constructor. This will internally call the constructor with parameter.

below is the code for the same.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InterviewCodePractice.CallConstructor
{
public class CallConstructor
{
public CallConstructor()
: this(5)
{
System.Windows.Forms.MessageBox.Show("Default");
}
public CallConstructor(int i)
{
System.Windows.Forms.MessageBox.Show("parameter");
}
}
}


Output of the above problem will be
1. parameter will be printed in the messagebox.
2. Default will be printed in the messagebox.

Upload File in .net

I was search for uploading the large file in asp.net and got the following article. I found this useful and that is why I am publishing the same on my blog.

Upload larger files
By default, ASP.NET permits only files that are 4,096 kilobytes (KB) (or 4 megabytes [MB]) or less to be uploaded to the Web server. To upload larger files, you must change the maxRequestLength parameter of the section in the Web.config file.

Note When the maxRequestLength attribute is set in the Machine.config file and then a request is posted (for example, a file upload) that exceeds the value of maxRequestLength, a custom error page cannot be displayed. Instead, Microsoft Internet Explorer will display a "Cannot find server or DNS" error message.

If you want to change this setting for all of the computer and not just this ASP.NET application, you must modify the Machine.config file.

By default, the element is set to the following parameters in the Machine.config file:
executionTimeout="90"
maxRequestLength="4096"
useFullyQualifiedRedirectUrl="false"
minFreeThreads="8"
minLocalRequestFreeThreads="4"
appRequestQueueLimit="100"
/>

The Machine.config file is located in the \System Root\Microsoft.NET\Framework\Version Number\Config folder.

Theoretically, the maximum file upload size is fairly large. However, because of ASP.NET health monitoring, you cannot upload very large files in ASP.NET. The ASP.NET worker process has a virtual address space of 2 gigabytes (GB). However, the ASP.NET worker process only uses a little more than 1 GB because of health monitoring and memory fragmentation.

During the upload process, ASP.NET loads the whole file in memory before the user can save the file to the disk. Therefore, the process may recycle because of the memoryLimit attribute of the processModel tag in the Machine.config file. The memoryLimit attribute specifies the percentage of physical memory that the ASP.NET worker process can exhaust before the process is automatically recycled. Recycling prevents memory leaks from causing ASP.NET to crash or to stop responding.

Additionally, other factors play a role in the maximum file size that can be uploaded. These factors include available memory, available hard disk space, processor speed, and current network traffic. With regular traffic of files being uploaded, Microsoft recommends that you use a maximum file size in the range of 10 to 20 megabytes (MB). If you rarely upload files, the maximum file size may be 100 MB.

Note You can upload files that are larger than 100 MB in ASP.NET. However, Microsoft recommends that you follow the maximum file upload sizes that are mentioned in this article. To determine more precise file sizes, perform stress testing on computers that are similar to the ones that will be used in production.

You may notice the following error messages if you encounter file size limits during the file upload process:
• The page cannot be displayed.
• Server Application is Unavailable
In the event log, the error message will be similar to the following:
aspnet_wp.exe (PID:PIDNumber) was recycled because memory consumption exceeded the SizeLimit MB (Percentage percent of available RAM).
• Exception of type System.OutOfMemoryException was thrown.
You may also find that uploads occur very slowly. If you watch the Aspnet_wp.exe process in Windows Task Manager, you will notice that the memory delta changes by 64 KB every 1 to 2 seconds. Depending on the size of the file, this delay may cause the ASP.NET worker process to recycle because of a responseDeadlock error.

Monday, November 10, 2008

Paging Procedure.

I was need to do the paging at run time and get the page specific data.
Below is the procedure for the same i found while surfing the net and i belive this is a grat findings.

CREATE PROCEDURE [dbo].[utilPAGE]
@datasrc nvarchar(200)
,@orderBy nvarchar(200)
,@fieldlist nvarchar(200) = '*'
,@filter nvarchar(200) = ''
,@pageNum int = 1
,@pageSize int = NULL
AS
SET NOCOUNT ON
DECLARE
@STMT nvarchar(max) -- SQL to execute
,@recct int -- total # of records (for GridView paging interface)

IF LTRIM(RTRIM(@filter)) = '' SET @filter = '1 = 1'
IF @pageSize IS NULL BEGIN
SET @STMT = 'SELECT ' + @fieldlist +
'FROM ' + @datasrc +
'WHERE ' + @filter +
'ORDER BY ' + @orderBy
EXEC (@STMT) -- return requested records
END ELSE BEGIN
SET @STMT = 'SELECT @recct = COUNT(*)
FROM ' + @datasrc + '
WHERE ' + @filter
EXEC sp_executeSQL @STMT, @params = N'@recct INT OUTPUT', @recct = @recct OUTPUT
SELECT @recct AS recct -- return the total # of records

DECLARE
@lbound int,
@ubound int

SET @pageNum = ABS(@pageNum)
SET @pageSize = ABS(@pageSize)
IF @pageNum < 1 SET @pageNum = 1
IF @pageSize < 1 SET @pageSize = 1
SET @lbound = ((@pageNum - 1) * @pageSize)
SET @ubound = @lbound + @pageSize + 1
IF @lbound >= @recct BEGIN
SET @ubound = @recct + 1
SET @lbound = @ubound - (@pageSize + 1) -- return the last page of records if -- no records would be on the
-- specified page
END
SET @STMT = 'SELECT ' + @fieldlist + '
FROM (
SELECT ROW_NUMBER() OVER(ORDER BY ' + @orderBy + ') AS row, *
FROM ' + @datasrc + '
WHERE ' + @filter + '
) AS tbl
WHERE
row > ' + CONVERT(varchar(9), @lbound) + ' AND
row < ' + CONVERT(varchar(9), @ubound)
EXEC (@STMT) -- return requested records
END

utilPAGE 'TableName', 'OrderBy', '*', '', 4, 10

@datasrc
- the table (or stored procedure, etc.) name
@orderBy
- the ORDER BY clause
@fieldlis
- the fields to return (including calculated ex-pressions)
@filter
- the WHERE clause
@pageNum
- the page to return (must be greater than or equal to one)
@pageSize
- the number of records per page