Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm working with a program that uploads to site folders, using C# and WSS3, and I am trying to ensure that it works correctly with Sharepoint permissions.

In my test program for the Frontpage RPC method which I am using to upload, which uses exactly the same classes for the actual functionality as my "live" program, when files are uploaded they appear with my full name next to them.

If I upload a file through my live application the uploaded file modified by name appears as MYDOMAIN\MYCOMPUTERNAME$ this isn't what I expected as I am logged on to an Active Directory account not the local computer account.

I have tried this using CredentialsCache.DefaultNetworkCredentials or .NetworkCredentials and new NetworkCredential(myUsername, myPass, myDomain) as credentials for the WebClient credentials, but all have the same result.

The only difference is the way in which they are being called upon, the test is a simple Forms project. The live version uses reflection to load up a script, which contains the same code as my test project. Aside from that they use the same method with the same Data so the results should be the same, although they aren't.

object o = results.CompiledAssembly.CreateInstance("Script");
Type type = o.GetType();
 MethodInfo m = type.GetMethod("Main");

try
{

Object returnVal = m.Invoke(o, new object[] { item.ScanDoc });

if (returnVal == null)
{
    executionSuccessful = true;
}
}

This doesn't fit my needs, as different users intended to have different permissions are likely to use the same computer. We also want to know who uploaded which documents.

I have an idea on how I can restrict users access to places they should't be able to upload to by calculating their permissions from the masks associated with their account that I can return via the web services.

I have no idea how I can change the "modified by" name or why reflection is causing a difference in result. Does anyone have any ideas what is causing it show my local computer name and how I can bypass this issue?

Edit

    public static String DoUpload(string destinationUrl, byte[] bytes, Dictionary<string, object> metaInfo)
    {
        string result = "";
        // Substring URL until and attempt communication until root site found.
        string webUrl = GetWebURL(destinationUrl);
        if (webUrl == "Access Denied")
        {
            throw new Exception("Access Denied - Verify your account has full control on target website.");
        }
        string documentName = destinationUrl.Substring(webUrl.Length + 1);
        return DoUpload(webUrl, documentName, bytes, metaInfo, out result);
    }

    public static String DoUpload(string webUrl, string documentName, byte[] bytes, Dictionary<string, object> metaInfo, out string result)
    {
        string putOption = "overwrite,createdir,migrationsemantics";  // See http://msdn2.microsoft.com/en-us/library/ms455325.aspx
        string comment = null;
        bool keepCheckedOut = false;
        // execute put doc method
        string method = "method=put+document%3a12.0.4518.1016&service_name=%2f&document=[document_name={0};meta_info=[{1}]]&put_option={2}&comment={3}&keep_checked_out={4}\n";
        method = String.Format(method, documentName, EncodeMetaInfo(metaInfo), putOption, HttpUtility.UrlEncode(comment), keepCheckedOut.ToString().ToLower());
        List<byte> data = new List<byte>();
        data.AddRange(Encoding.UTF8.GetBytes(method));
        data.AddRange(bytes);
        try
        {
            using (WebClient webClient = new WebClient())
            {
                webClient.Credentials = CredentialCache.DefaultNetworkCredentials;
                webClient.Headers.Add("Content-Type", "application/x-vermeer-urlencoded");
                webClient.Headers.Add("X-Vermeer-Content-Type", "application/x-vermeer-urlencoded");
                result = Encoding.UTF8.GetString(webClient.UploadData(webUrl + "/_vti_bin/_vti_aut/author.dll", "POST", data.ToArray()));
            }
        }
        catch (Exception ex)
        {
            result = ex.Message;
        }
        return result;
    }

Edit2

Looking into cause of highlighted differences now. Highlighted Differences Between Responses

share|improve this question
    
can you add some code your using for uploading a document and what credentials your using for that operation –  Muhammad Raja Aug 14 '12 at 10:00
    
@TimeToThine I'm adding my upload helper class now. As for the credentials I am using, I have tried everything from the cache and adding the hard coded name/pass (NTLM) as described in my question. –  Amicable Aug 14 '12 at 10:12
    
I've not been able to move further forward, I've been struggling to find more information on the Vemeer packet responses. I've been using ULSViewer to see if there was any connection between the Sharepoint log messages and my differing responses. –  Amicable Aug 20 '12 at 10:08
add comment

2 Answers

You can modify the audit fields if you use the Administrator account. I think the item's owner would also work.

share|improve this answer
add comment

According to http://blog.slalom.com/2011/05/13/using-vermeer-content-types-and-impersonation-to-handle-sharepoint-document-uploads/, they are using the same code you are with one exception - instead of the List data = new List(); they are using var data = new List(). CredentialCache.DefaultCredentials is what you should be using.

Also, depending what you are using to initiate the add you may need to elevate the privileges.

share|improve this answer
    
Great find, the blog in which I discovered this method does not explain it the process anywhere near as well. –  Amicable Aug 14 '12 at 15:17
    
On second thought, how does he get data = new List() to compile without a type? Even List<object> shouldn't work in that manner either as it can't infer a type. –  Amicable Aug 14 '12 at 16:31
    
I haven't tried the sample code, but I agree that it would require the type. As far as other similar samples, I've also found silver-it.com/node/114. Have you tried setting up to check for the message successful? and what kind of results are you getting? –  Quinn Johns Aug 14 '12 at 17:49
    
I perform the same success check at a lower point in my code. This did lead me to noticing a few differences in the response that I had missed earlier, that I am now looking into. –  Amicable Aug 15 '12 at 8:29
    
How's that coming? –  Quinn Johns Aug 16 '12 at 18:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.