Archive for July, 2012
Difference between ‘Shadowing’ and ‘Overriding’ (Shadowing vs Overriding) – C#
Posted by Rajanihanth in .Net, C#, Tech Tips-.Net, Versus (vs) on July 29, 2012
This is a very quick Tech-Tips..! Actually Shadowing is VB.Net concept, in C# this concept called hiding! We will see this chapter later. 🙂
Shadowing : Creating an entirely new method with the same signature as one in a base class.
Overriding: Redefining an existing method on a base class.
- Both are used when a derived class inherits from a base class
- Both redefine one or more of the base class elements in the derived class
If you want to read more regarding this, just follow this MSDN topic.
class A { public int M1() { return 1; } public virtual int M2() { return 1; } } class B : A { public new int M1() { return 2; } public override int M2() { return 2; } } class Program { static void Main(string[] args) { B b = new B(); A a = (A)b; Console.WriteLine(a.M1()); //Base Method Console.WriteLine(a.M2()); //Override Method Console.WriteLine(b.M1()); Console.WriteLine(b.M2()); Console.Read(); } }
The output is : 1, 2, 2, 2
SecurityException was unhandled by user code – VSTA, C#, InfoPath 2007
Posted by Rajanihanth in .Net, Active Directory, DirectoryServices, InfoPath 2007, InfoPath 2010, VSTA on July 18, 2012
I got this error, while I was developing a InfoPath form in VSTA (Visual Studio Tools for Applications 2008) . This was my enviornment:
- Windows 7
- VSTA 2008
- InfoPath 2007
Scenario: I was trying to get the user information from Active Directory(AD) and then display in an InfoPath form. Here is the C# code I have written in the VSTA:
public void CTRL1_5_Clicked(object sender, ClickedEventArgs e) { string strUserName = System.Environment.UserName; string xpath2 = "/my:myFields/my:field1"; XPathNavigator field2 = MainDataSource.CreateNavigator().SelectSingleNode(xpath2, NamespaceManager); field2.SetValue(GetOU(strUserName)); //Get Organization Unit(OU) from Acitve Directory(AD) }
This is the error message I have got: 😦
Solution: I have fixed this issue in a different situation, just read in my previous post.
Thanks. R./
Happy InfoPathing. 🙂
InfoPath cannot grant access to these files and settings because the form template is not fully trusted. For a form to run with full trust, it must be installed or digitally signed with a certificate – InfoPath 2007 forms Security Levels
Posted by Rajanihanth in .Net, Active Directory, DirectoryServices, InfoPath 2007, InfoPath 2010 on July 18, 2012
After giving the Full Trust to the Form template (See here), we will be able to preview the InfoPath form and get information from other domains, or access files and settings on a user’s computer. But when we publish InfoPath form on SharePoint and try to access the from then you will be getting this error. 😦
Error:
Details:
Text format of the error:
Form template: file:///C:\Documents%20and%20Settings\RajanihanthV\Desktop\XmlProc\InfoPath\ADInfo.xsn</pre> The form template is trying to access files and settings on your computer. InfoPath cannot grant access to these files and settings because the form template is not fully trusted. For a form to run with full trust, it must be installed or digitally signed with a certificate.
If we need to access the form from SharePoint, we need to be digitally signed the form even it is fully trusted. This is pretty simple, but we have to do this process after publishing the form on SharePoint only. Here are the steps.
Step 1: Navigate to the Document (form) Library, where your form published
Step 2: Click Settings on the Document Library Settings
Step 3: Advanced Settings
Step 4: Edit Template
Step 5: Click the Form Options on the Tools menu
Step 6: Click Sign this Form Template and create or select certificate, then OK
That’s all. You can try and see. 🙂
This will work only for you (author), If you want give access to all users you have to get the digital signature from 3rd party vendors and signed the form.
Thanks. R./
Difference between ‘ref’ and ‘out’ parameters (ref vs out) – C#
Posted by Rajanihanth in .Net, Tech Tips-.Net, Versus (vs) on July 14, 2012
- Both are treated differently @ runtime but same in the compile time, so can’t be overloaded.
- Both are passed by references except ‘ref’ requires that the variable to be initialized before passed.
ref:
class Program { static void M1(out int i) { i = 1; i += 1; } static void Main(string[] args) { int j; //Not assigned M1(out j); Console.Write(j); Console.Read(); } } }
The output is : 2
out:
class Program { static void M1(ref int i) { i = 1; i += 1; } static void Main(string[] args) { int j=0; //Assigned M1(ref j); Console.Write(j); Console.Read(); } }
The output is same: 2, but we have to assigned the variable before used otherwise we will get an error.
‘Microsoft.SharePoint.Client.Form’ does not contain a constructor that takes 0 arguments – SharePoint 2010 Client Object Model
Posted by Rajanihanth in .Net, Client Object model, SharePoint 2010 on July 13, 2012
To fix this issue, just click here. Thanks, R../
How to get the list of folders and subfolders from a SharePoint document library
Posted by Rajanihanth in SharePoint 2007, SharePoint 2010, Tech Tips-SharePoint on July 13, 2012
There are many ways to get files & folder details from SharePoint (programmatically) but my manager asked me to provide the list of folders and sub folders from a SharePoint document library urgently. So I wanted get an idea to provide the information quickly, after spending few minutes on the web and found this post (I know about the DIR command but didn’t think about the SharePoint UNC). Yes, we can get the details using DOS command with UNC path. 🙂
First of all, we want to know about UNC and the DIR command.
UNC: Universal Naming Convention, a format for specifying the location of resources on a local-area network (LAN). Read more about this here.
Syntax: \\<servername>\<sharename>\<directory>
DIR: Display a list of files and subfolders. Read more about this here.
Syntax: DIR [pathname(s)] [display_format] [file_attributes] [sorted] [time] [options]
To get SharePoint UNC path, just remove http: from the SharePoint url and replace the forward slash with backslash. So my document library path is:
http://ServerName/doccenter/Administration/StepBiStep
then it’s UNC will be like this:
\\ServerName\doccenter\Administration\StepBiStep
Okay.. we will see the steps here, I have created a document library (StepBiStep) for testing purpose.
Step 1: Open the cmd prompt
Step 2: type the following statement in the command-line and press Enter key.
DIR /B /A:D / S [UNC Path] > [File Name]
– /B -Bare format (no heading, file sizes or summary)
– /A:D -Folder file attributes
– /S -Include all subfolders
– [UNC Path] -The path, which you want to get all the folders and subfolders
– [File Name] -A text file name to store the folder details
So our command is looks like this, I have used a text file (FolderLists.txt, which is located in C: drive) to store the details.
DIR /b /A:D /S \\ServerName\doccenter\Administration\StepBiStep > c:\FolderLists.txt
Step 3: It will take few minutes to generate the list. You can open in a Notepad and see..!.
That’s all guys, enjoy! I do welcome your comments. Please leave them here! 🙂
References:
‘Form’ is an ambiguous reference between ‘System.Windows.Forms.Form’ and ‘Microsoft.SharePoint.Client.Form’ – SharePoint 2010 Client Object Model
Posted by Rajanihanth in .Net, Client Object model, SharePoint 2010 on July 13, 2012
When you try to use the Client Object Model (SharePoint 2010) with Windows Application in Visual Studio 2010, you might be getting these error messages. (You can find the step by step instructions to use the SharePoint 2010 Client Object Model here)
Error Message:
'Form' is an ambiguous reference between 'System.Windows.Forms.Form' and 'Microsoft.SharePoint.Client.Form'
Reason: The reason is obvious and we can get from the error message itself. Yes, after added the SharePoint Client reference to the code, there is a conflict between ‘System.Windows.Forms.Form’ and ‘Microsoft.SharePoint.Client.Form’! Initially our Form1 class inherits from Form class (by default System.Windows.Forms.Form) and currently our program confuses to choose the correct reference.
Solution: Provide full reference to the Form class will be the solution for this issue, so replace ‘Form‘ with ‘System.Windows.Forms.Form‘
After Replacing the code:
public partial class Form1 : System.Windows.Forms.Form { public Form1() { InitializeComponent(); } }
That’s all guys, Happy Programming with Client Object Model.
Thanks, R../
The type or namespace name ‘SharePoint’ does not exist in the namespace ‘Microsoft’ (are you missing an assembly reference?) – SharePoint 2010 Client Object Model dlls
Posted by Rajanihanth in .Net, Client Object model, SharePoint 2010 on July 13, 2012
When I started using the Client Object model (Client OM) in SharePoint 2010, I have got this simple error and I just want to share with you in this blog. Following was my develpment environment.
- Windows 7, 64 bit
- Console Application Visual Studio 2010
- Added Client Object Model references (Microsoft.SharePoint.Client.dll & Microsoft.SharePoint.Client.Runtime.dll) to the solution
After setting up all the above, I was trying to add the client reference to the code ‘using Microsoft.SharePoint.Client;‘and then I have got the following error and warnings 🙂
The text format of error is:
"The type or namespace name 'SharePoint' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)"
It’s an obvious error message and the reason of this error is:
SharePoint 2010 does not support .Net 4.0 in default and Visual Studio 2010 default framework is .Net 4.0.
Here are the simple steps to fix this:
Step 1: Right click on the project –> Properties
Step 2: Change the Target framework –> .Net Framework 3.5
Step 3: Warning message will be popping up, click Yes.
That’s all. Happy Coding..!
Please Note: If you use Windows Application then you will be getting an other error message! 😦 To solve this issue just click here.