Joeygurl’s Trial & Errors with Possible Solutions











So I’ve developed an automation tool using C# to print out InfoPath documents from my SharePoint Document Library. Here are the steps on how my is printing:

  1. Capture the path of the infopath document. e.g. http://mysharepointsite/form/form1.xml
  2. Create an instance of “Microsoft.Office.Interop.InfoPath.Application”
  3. Open the infopath document using the instance
  4. Used the instance’s XDocument.PrintOut() function.
  5. Used Thread.Sleep() to put a gap in between closing and opening a new form.
  6. Close the instance.

It works fine at times but sometimes when my software testers run my program, the program would randomly stop in the middle of printing. I’ve captured the logs and found that it was throwing this error:

Creating an instance of the COM component with CLSID {8075535F-5146-11D5-A672-00B0D022E945} from the IClassFactory failed due to the following error: 80010108.

It happened whenever I had multiple forms printed out. It seems that the threading interval wasn’t enough at times. So I’ve added code that called a recursive function to check whether an InfoPath instance is still running and add interval to the sleep thread.

So I would like to share this code since there isn’t much about this out there, hope it helps!

 
//Print Method
private static void PrintForm(string sourceDocument)
        {
            Microsoft.Office.Interop.InfoPath.Application appIP=null;
            object varIndex = null;
            try
            {
                //Check if an instance of InfoPath is already running.
                WaitForInfoPathToEnd();

                appIP = new Microsoft.Office.Interop.InfoPath.Application();

                varIndex = appIP.XDocuments.Open(sourceDocument, (int)Microsoft.Office.Interop.InfoPath.XdDocumentVersionMode.xdUseExistingVersion);

                if (appIP.XDocuments.Count > 0)
                {
                         appIP.Windows[0].XDocument.PrintOut();
                 }
            }
            catch (Exception ex)
            {
                //Do something
            }
            finally
            {
                Thread.Sleep(4000);
                if (varIndex != null)
                    appIP.XDocuments.Close(varIndex);
                Thread.Sleep(2000);
                if (appIP != null)
                {
                    appIP.Quit(true);
                    appIP = null;
                }
            }
        }

//Check if an instance of infopath instance is still running
 private static void WaitForInfoPathToEnd()
        {
            Process[] processes = Process.GetProcessesByName("INFOPATH");
            if (processes.Length > 0)
            {
                Thread.Sleep(4000);
                WaitForInfoPathToEnd();
            }

        }


I’ve been having this dilemna on how to programmatically add a document (in my case an infopath form) to a SharePoint document library. I’ve seriously searched all over google using all the jargons I could think of regarding this matter. It would’ve been easier if my development computer was a Windows Server 2003 OS because it would be as easy as using the “SharePoint.dll” classes that came with WSS. But since I was given XP OS to work with, I had to find ways to make things happen magically at work. Without the power of “SharePoint.dll”, I had to remotely connect to the SharePoint development server and utilize the built-in Web Services it came with to manipulate the SharePoint Site.

So after aeons of research, I finally came across this guy – Namwar’s blog:

http://sharepointinsight.wordpress.com/2009/01/10/programmatically-upload-a-file-to-document-library/

In case, he decides to cancel his blog site, I’ll paste the solution here:

Following is a utility function which you can use to upload a file programmatically in SharePoint document library. It has two parameters. First is the source file path and second is the target document library path.

Following is an example call to this function:

UploadFileToDocumentLibrary(@”C:\test.txt”, @”http://home-vs/Shared Documents/textfile.txt”);

and here is the function

public static bool UploadFileToDocumentLibrary(string sourceFilePath, string targetDocumentLibraryPath)
{
//Flag to indicate whether file was uploaded successfuly or not
bool isUploaded = true;
try
{
// Create a PUT Web request to upload the file.
WebRequest request = WebRequest.Create(targetDocumentLibraryPath);
//Set credentials of the current security context
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = “PUT”;
// Create buffer to transfer file
byte[] fileBuffer = new byte[1024];
// Write the contents of the local file to the request stream.
using (Stream stream = request.GetRequestStream())
{
//Load the content from local file to stream
using (FileStream fsWorkbook = File.Open(sourceFilePath, FileMode.Open, FileAccess.Read))
{
//Get the start point
int startBuffer = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length);
for (int i = startBuffer; i > 0; i = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length))
{
stream.Write(fileBuffer, 0, i);
}
}
stream.Close();
}
// Perform the PUT request
WebResponse response = request.GetResponse();
//Close response
response.Close();
}
catch (Exception ex)
{
//Set the flag to indiacte failure in uploading
isUploaded = false;
}
//Return the final upload status
return isUploaded;
}

I actually had to re-define some lines because in my case, I had to grab the sourceFile from a URL and not from a local directory. He used “FileStream” which worked for files locally so I basically had to use “Stream” and WebClient. You’ll need to add the namespace “System.Net” so you can use WebClient. Below is my updated code:

public static bool UploadFileToDocumentLibrary(string sourceFilePath, string targetDocumentLibraryPath)
{
//Flag to indicate whether file was uploaded successfuly or not
bool isUploaded = true;
try
{
// Create a PUT Web request to upload the file.
WebRequest request = WebRequest.Create(targetDocumentLibraryPath);
//Set credentials of the current security context
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = “PUT”;
// Create buffer to transfer file
byte[] fileBuffer = new byte[1024];
// Write the contents of the local file to the request stream.
using (Stream stream = request.GetRequestStream())
{
//Load the content from local file to stream
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential(UserName, Password, Domain);
using (Stream fsWorkbook = wc.OpenRead(sourceFilePath))
{
//Get the start point
int startBuffer = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length);
for (int i = startBuffer; i > 0; i = fsWorkbook.Read(fileBuffer, 0, fileBuffer.Length))
{
stream.Write(fileBuffer, 0, i);
}
}
stream.Close();
}
// Perform the PUT request
WebResponse response = request.GetResponse();
//Close response
response.Close();
}
catch (Exception ex)
{
//Set the flag to indiacte failure in uploading
isUploaded = false;
}
//Return the final upload status
return isUploaded;
}

I hope this helps you guys! :-)



Did your Wireless Connection simply disappear on you without any obvious reason at all? If one day, you woke up and the first thing you do is check your email or surf the net and for some reason, your wireless connection couldn’t find any networks at all and yet it worked just the night before… You probably now have the WTF?! face… :-)

Anyway, I  was able to resolve this wireless issue today. Hopefully it works for you guys too.

I uninstalled my HP Wireless Assistant because I read somewhere that it was the one that triggers the shutdown of the wireless card.

Anyway, right-click My Computer and click manage. Go to Services and look for Net Driver HPZ12. This service should be Started and in Automatic mode. If it isn’t, then right click it, click on properties, pick Automatic from the list, click on apply then click on Start. Afterwards, make sure you restart your laptop. Once your laptop is on, make sure your wireless is on as well(look for little wireless sliding button, in my case it was on the right front panel of my laptop… blue light=on, orange light=off).

Hopefully this helps…  :-)



So I publish an InfoPath form to a sharepoint site and try to test it by clicking on the ‘Fill Out Form’ link. It loads… it loads then *BAM*!!! I get the error below.

iperr031

Error Details:

InfoPath cannot create a new, blank form. InfoPath cannot open the form. To fix this, problem. contact your system administrator.

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.

My Solution:

Apparently, it requires a certificate. So I just went back to design mode of  InfoPath source file and clicked on:

Tools>Form Options>Security, check the box “Sign this form” and Select or Create a Certificate.

Then re-publish! VOILA!



iperr01

Ever get that annoying little alert above whenever you try to switch to preview mode in InfoPath? Obviously I did! :-p

To get rid of it, go to Tools>Form Options>Security, check the box “Sign this form” and Select or Create a Certificate.



I’ve encoutered this using InfoPath 2003 & Visual Studio.NET 2003… Does it work for 2007 & 2.0? Not sure, but try it out anyway…

In this scenario, I have code-behind my InfoPath forms and wanted to step through it. As soon as I hit F5 and it compiles successfully, I get an error saying:

Unable to attach ‘INFOPATH.EXE’ (PID: 3384) on machine ‘<your-machine-name>’ .

iperr02

Ugh! Don’t you just hate roadblocks?! Anyway, here’s what I did… Open notepad or whatever text editor you are using. Copy & paste the tags below:

<configuration>
    <startup>
        <requiredRuntime imageVersion=”v1.1.4322″ version=”v1.1.4322″ />
        <supportedRuntime version=”v1.1.4322″ />
    </startup>
</configuration>

Save your file as ‘INFOPATH.EXE.CONFIG’ in the same  folder your ‘INFOPATH.EXE’ resides. In my case, it was under:

C:\Program Files\Microsoft Office\ OFFICE11

Hope this helps…

-D



et cetera