Sharepoint (WSS) authentication issues in code/web service

K

Ken McAndrew

I'm working with a client that has Windows Server 2003, WSS, accessed
through a custom application. In this application, we need to attach files
to cases by adding them to a database and storing them in Sharepoint. This
process works fine at our office, but the client is having authentication
problems.

The only authentication being used is Windows security. Before, my
Sharepoint code (in C#) was in a file, and the users would get a login
prompt. However, even if they entered correct information, it would not let
them in and eventually give an Unauthorized (401.1) error. I decided to move
the functionality to a web service and use the Credentials object to set up
authentication. These are the calls in my web page:

pmdtc_ws.CreateSite svcCreateSite = new PMDTC.pmdtc_ws.CreateSite();
svcCreateSite.Credentials =
System.Net.CredentialCache.DefaultCredentials;
string strResult = svcCreateSite.AttachFile(subWeb, sFileName,
pFileContents);

And this is the web service's method:

[WebMethod]
public string AttachFile(string pFolderID, string pFileName, byte[]
pFileContents)
{
string strMessage;

try
{
SPSite site = SPControl.GetContextSite(Context);
SPWeb web;// = SPControl.GetContextWeb(Context);
string tmpSite = site.Url;

if (pFolderID != "0")
{
tmpSite += "/" + pFolderID + "/";
}

tmpSite = tmpSite.Substring(7);
int posSite = tmpSite.IndexOf("/");
tmpSite = tmpSite.Substring(posSite);

web = site.OpenWeb(tmpSite);
web.AllowUnsafeUpdates = true;
SPFolder folder;
SPFile file;
folder = web.GetFolder("Shared Documents");

file = web.GetFile(folder.Url + "/" + pFileName);

if (file.Exists) ////check in existing file
{
file.CheckOut();
file.SaveBinary(pFileContents);
file.CheckIn(""); ////We have to force the checkin here.
}
else ////Add a new file
{
SPFileCollection files;
files = folder.Files;

files.Add(folder.Url + "/" + pFileName, pFileContents);
}

strMessage = "Success!";
}
catch (Exception ex)
{
strMessage = "Fail! " + ex.Message.ToString();
}

return strMessage;
}

When I run this, I get back an ASP.Net error page, that says the user is
unauthorized: 401, treating it as an unhandled exception. (As a note, users
have Contributer permissions in Sharepoint, and a cross-site group with
Contributer permissions is set up for the subsites that files might be
stored to, but this happens at any level.)

We found that if the administrator does a file attach, it will work, and
allow access to the site for a time, before it loses that access. Also, when
I used a standard user (having the problem) to access the Sharepoint site,
we got this error message:

"Error

List does not exist

The page you selected contains a list that does not exist. It may have been
deleted by another user. Click "Home" at the top of the page to return to
your Web site."

There was a fix mentioned on Google for this, giving users read access to
the webtemp.xml file in Templates/1033/XML, but that worked only until we
rebooted the machine.

I've been trying to figure this out for a few weeks now with no success, so
any help would be extremely appreciated. Thanks.
 
M

Mike Walsh

Please do not post WSS Programming questions to both the WSS newsgroups.

WSS Programming questions go only to .windowsservices.development and
non-'WSS Programming' questions go only to the main newsgroup
..windowsservices

It's a useful division that saves everyone a lot of time in finding messages
they are interested in.

Thanks for your assistance with this when posting again.

(Follow-up changed)

Mike Walsh, Helsinki, Finland
WSS FAQ http://wss.collutions.com
please reply to the newsgroup only

Ken McAndrew said:
I'm working with a client that has Windows Server 2003, WSS, accessed
through a custom application. In this application, we need to attach files
to cases by adding them to a database and storing them in Sharepoint. This
process works fine at our office, but the client is having authentication
problems.

The only authentication being used is Windows security. Before, my
Sharepoint code (in C#) was in a file, and the users would get a login
prompt. However, even if they entered correct information, it would not
let them in and eventually give an Unauthorized (401.1) error. I decided
to move the functionality to a web service and use the Credentials object
to set up authentication. These are the calls in my web page:

pmdtc_ws.CreateSite svcCreateSite = new PMDTC.pmdtc_ws.CreateSite();
svcCreateSite.Credentials =
System.Net.CredentialCache.DefaultCredentials;
string strResult = svcCreateSite.AttachFile(subWeb, sFileName,
pFileContents);

And this is the web service's method:

[WebMethod]
public string AttachFile(string pFolderID, string pFileName, byte[]
pFileContents)
{
string strMessage;

try
{
SPSite site = SPControl.GetContextSite(Context);
SPWeb web;// = SPControl.GetContextWeb(Context);
string tmpSite = site.Url;

if (pFolderID != "0")
{
tmpSite += "/" + pFolderID + "/";
}

tmpSite = tmpSite.Substring(7);
int posSite = tmpSite.IndexOf("/");
tmpSite = tmpSite.Substring(posSite);

web = site.OpenWeb(tmpSite);
web.AllowUnsafeUpdates = true;
SPFolder folder;
SPFile file;
folder = web.GetFolder("Shared Documents");

file = web.GetFile(folder.Url + "/" + pFileName);

if (file.Exists) ////check in existing file
{
file.CheckOut();
file.SaveBinary(pFileContents);
file.CheckIn(""); ////We have to force the checkin here.
}
else ////Add a new file
{
SPFileCollection files;
files = folder.Files;

files.Add(folder.Url + "/" + pFileName, pFileContents);
}

strMessage = "Success!";
}
catch (Exception ex)
{
strMessage = "Fail! " + ex.Message.ToString();
}

return strMessage;
}

When I run this, I get back an ASP.Net error page, that says the user is
unauthorized: 401, treating it as an unhandled exception. (As a note,
users have Contributer permissions in Sharepoint, and a cross-site group
with Contributer permissions is set up for the subsites that files might
be stored to, but this happens at any level.)

We found that if the administrator does a file attach, it will work, and
allow access to the site for a time, before it loses that access. Also,
when I used a standard user (having the problem) to access the Sharepoint
site, we got this error message:

"Error

List does not exist

The page you selected contains a list that does not exist. It may have
been deleted by another user. Click "Home" at the top of the page to
return to your Web site."

There was a fix mentioned on Google for this, giving users read access to
the webtemp.xml file in Templates/1033/XML, but that worked only until we
rebooted the machine.

I've been trying to figure this out for a few weeks now with no success,
so any help would be extremely appreciated. Thanks.

--

Kenneth S. McAndrew
Software Developer, Information Concepts
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top