Tuesday, 6 August 2013

SharePoint Claims-Authentication, Cookies, C#, and Excel-DLL/tld

SharePoint Claims-Authentication, Cookies, C#, and Excel-DLL/tld

I'm quite new to C# and SharePoint, so I'm bouncing around the net trying
to get this problem resolved. I'm on the last step, which means that when
I get this issue resolved, I can get the program over to my client and
move on to less frustrating projects using online systems that, unlike
SharePoint, don't suck.
The basic concept of the project is it accesses SharePoint, verifies a
certain file exists, verifies the file is NOT locked by another user, and
then, if those two conditions are met, excel opens the file up and does
some stuff with it, then saves the file and closes it again.
The issue is at the point where the program accesses SharePoint and runs
through the verification steps. Initially, my first crack at this was
directly from VBA using SOAP, and, until Microsoft decided to downgrade
SharePoint to 2013 from 2010, it worked quite well, meaning now, I'm into
using Visual Studio and C# to accomplish the same thing that SOAP was
doing in SP2010.
I've attempted implementing the code found here:
http://www.vrdmn.com/2013/01/authenticating-net-client-object-model.html,
however I can't get it to work. What I did manage to get working was the
2010 authentication model which pops up a browser window, checks for valid
cookies, and, if it finds them, reads the cookies, otherwise prompts the
user to log into SharePoint and then closes the browser and continues on.
public CookieCollection Show()
{
if (string.IsNullOrEmpty(this.LoginPageUrl)) throw new
ApplicationException(Constants.MSG_NOT_CLAIM_SITE);
// navigate to the login page url.
this.webBrowser.Navigate(this.LoginPageUrl);
DisplayLoginForm = new Form();
DisplayLoginForm.SuspendLayout();
// size the login form
int dialogWidth = Constants.DEFAULT_POP_UP_WIDTH;
int dialogHeight = Constants.DEFAULT_POP_UP_HEIGHT;
if (PopUpHeight != 0 && PopUpWidth != 0)
{
dialogWidth = Convert.ToInt32(PopUpWidth);
dialogHeight = Convert.ToInt32(PopUpHeight);
}
DisplayLoginForm.Width = dialogWidth;
DisplayLoginForm.Height = dialogHeight;
DisplayLoginForm.Text = this.fldTargetSiteUrl;
DisplayLoginForm.Controls.Add(this.webBrowser);
DisplayLoginForm.ResumeLayout(false);
//DisplayLoginForm.Show();
Application.Run(DisplayLoginForm);
// see ClaimsWebBrowser_Navigated event
//DisplayLoginForm.Dispose();
return this.fldCookies;
}
The problem comes about with the Application.Run(DisplayLoginForm). If I
step through the code line-by-line, everything works fine, and I get the
results I need within my VBA code. However, if I run the program with F5
(either by building it in Debug mode or in Release mode),
Application.Run(DisplayLoginForm) kills the application (and Excel along
with it) once the cookie jar (my term, and most likely not a computer
term) has been examined for valid cookies.
You can see in the code, I attempted to use DisplayLoginForm.Show();
rather than Application.Run, however I kept getting null references to the
file I'm trying to find, so that approach does not work either.
So here's the question:
How do I go about popping up a web browser (which is obviously set up as a
windows form), look for the cookies, prompt the user if needed, close the
web browser and remain alive long enough to return the appropriate values
(file.exists and file.islockedbyuser.email, both of which are in functions
called by the VBA and both of which work just fine) to excel, and then
finish up the program without shutting Excel down in the process?

No comments:

Post a Comment