Tuesday, May 11, 2010

How to check if a double or integer or any numeric value is NULL

If you try to check on .NET using condition line

If myInteger Is Nothing=True Then

'Print something here

End If

Likely if the value of the integer is 0 (instead of real null value) the condition will become true and the code inside if statement will be executed. This is very fraustrating if the real purpose of the code is to check if the particular variable is really NULL

The better way to work around is to declare your integer as

Dim myInteger as Nullable(Of Integer)

or if it is double

Dim myDouble as Nullable(Of Double)

If you try to do the NULL test again using the code above, this time if the integer has value 0 the statement won't take it as true. Only if the integer is really NULL value the statement will be executed.

Just a little simple tricks. ;) If you don't know how to do it, it will take you about 10-20 minutes or so googling around and figuring out how this can be done.. justl ike me.. =D

How to copy file using copy.asmx sharepoint web service

To copy files first we need to add a web service in Visual Studio 2008. The link to add should be

http://sharepoint/sites/yoursite/_vti_bin/Copy.asmx

Just append the /_vti_bin/Copy.asmx behind the sharepoint sites that we need to copy file.

The next step is to configure NTLM authentication for sharepoint. (I assume you use the default NTLM authentication instead of the Kerberos. You will have to change the WCF configuration as follow in binding section

<binding name="CopySoap" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="67108864" maxBufferPoolSize="67108864" maxReceivedMessageSize="67108864"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="10000000" maxStringContentLength="10000000"
maxArrayLength="67108864" maxBytesPerRead="65536" maxNameTableCharCount="100000" />
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>

Take note of the clientCredentialType="Ntlm" setting at transport credential.

The following code will enable the user to copy file from 1 location to another

CopyIntoItemsLocal function did the copying of the file. If you want to copy the file from sharepointserver 1 to sharepointserver2 this method cannot be used. You have to use CopyIntoItems method instead. For my case, I am copying file within the same sharepoint server. So the code as follow

Imports System.Net
Imports System.IO
Imports System
Module Module1

Sub Main()

Dim copyService As CopyWebService.CopySoapClient = New CopyWebService.CopySoapClient()
copyService.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation
copyService.ClientCredentials.Windows.ClientCredential.Domain = "yourdomain"
copyService.ClientCredentials.Windows.ClientCredential.UserName = "adminuser"
copyService.ClientCredentials.Windows.ClientCredential.Password = "yourpassword"

Dim myCopyResult1 As New CopyWebService.CopyResult
Dim myCopyResultArray As CopyWebService.CopyResult() = {myCopyResult1}

Try

Dim copySource As String = "http://sharepoint/sites/mysite/Report/Test.xlsx"
Dim copyDest As String() = {"http://sharepoint/sites/mysite/Report/TestCopyFile.xlsx"}
Dim myUint As System.UInt32 = copyService.CopyIntoItemsLocal(myCopyResultArray, copySource, copyDest)
If myUint = 0 Then
Dim idx As Integer = 0
Dim myCopyResult As CopyWebService.CopyResult
For Each myCopyResult In myCopyResultArray
Dim opString As String = (idx + 1).ToString()
If myCopyResultArray(idx).ErrorMessage Is Nothing Then
Console.WriteLine("Copy Operation Ok")
Else
Console.WriteLine("Copy Operation fail")
End If
idx += 1
Next myCopyResult
End If
Catch exc As Exception
Dim idx As Integer = 0
Dim myCopyResult As CopyWebService.CopyResult
For Each myCopyResult In myCopyResultArray
idx += 1
If myCopyResult.DestinationUrl Is Nothing Then
Dim idxString As String = idx.ToString()
Console.WriteLine("Copy operation failed." + _
myCopyResult.ErrorMessage + myCopyResult.ErrorCode + _
"Description: " + exc.Message, "Exception")
End If
Next myCopyResult
End Try

End Sub

End Module