none
How to pass credentials of an SPSite? RRS feed

  • Question

  • I am running c# code on farm1 to get user profile picture from mysite web app which is hosted in farm2.

    siteUrl = "https://mysite";

    url = "https://mysite/User%20Photos/Profile%20Pictures/USER1_SThumb.jpg"

    using (SPSite site = new SPSite(siteUrl, SPUserToken.SystemAccount))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            file = web.GetFile(url);
                            if (file.Exists)
                            {
                                strBase64 = "\n" + Convert.ToBase64String(file.OpenBinary()) + "\n";
                            }
                            else
                            {
                                //get Default image base64 sample
                                strBase64 = "\n/9j/4AAQSkZJRgABAQEAYABg........oA//2Q==\n";
                            }
                        }
                    }

    new SPSite(siteUrl, SPUserToken.SystemAccount) gives me system.io.filenotfoundexception. the web application https://mysite could not be found. Verify that you have typed the url correctly. if the url should be serving existin content, the system administrator may need to add a new request url mapping to the intended application.

    I am running this code on http://webapp1 and it cannot reach the mysite webapp. Please advise how to pass the credentials. Note that the Timer service account of webapp1 has read access to mysiteqa and all user pictures

    I don't want to type the password/username in the code. Both web applications are using windows authentication.

    • Edited by MC ROY Monday, October 7, 2019 10:40 AM
    Monday, October 7, 2019 10:26 AM

Answers

All replies

  • Why do you want to hit some other site or server to get user's profile photo when you can do it within the current site itself without need for authentication. Use this url directly:

    https://current-site/_layouts/15/userphoto.aspx?size=L&username=joao@handsonsharepoint.com
    It should return the same picture.

    Here is the reference article:

    https://sharepoint.handsontek.net/2018/10/05/how-to-get-sharepoint-profile-picture/

    Thanks


    Please remember to up-vote & mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    • Edited by HarshalGite Monday, October 7, 2019 12:23 PM
    Monday, October 7, 2019 12:22 PM
  • Hi MCRoy,

    You cannot access resources of farm 2 from farm 1 using SOM. For this purpose we have CSOM. Here is a sample link for your reference - 

    https://sharepoint.stackexchange.com/questions/62087/how-to-get-a-file-using-sharepoint-client-object-model-with-only-an-absolute-url


    K Mohit

    Monday, October 7, 2019 7:21 PM
  • Hi,

    If you want to get file from another farm, please use CSOM C# code to achieve it, we have to use username/password to pass the credentials.

    The following code for your reference.

    var siteUrl = "https://mysite";
    var fileUrl = "/User%20Photos/Profile%20Pictures/USER1_SThumb.jpg";
    var strBase64 = "";
    using (ClientContext clientContext = new ClientContext(siteUrl))
    {
    	clientContext.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
    	var file = clientContext.Web.GetFileByServerRelativeUrl(fileUrl);
    	clientContext.Load(file);
    	try
    	{
    		clientContext.ExecuteQuery();
    		ClientResult<System.IO.Stream> data = file.OpenBinaryStream();
    		clientContext.ExecuteQuery();
    		using (System.IO.MemoryStream mStream = new System.IO.MemoryStream())
    		{
    			if (data != null)
    			{
    				 
    				data.Value.CopyTo(mStream);
    				byte[] imageArray = mStream.ToArray();
    				strBase64 = "\n" + Convert.ToBase64String(imageArray) + "\n";
    			}
    		}
    	}
    	catch
    	{
    		//get Default image base64 sample
    		strBase64 = "\n/9j/4AAQSkZJRgABAQEAYABg........oA//2Q==\n";
    	}                             
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Tuesday, October 8, 2019 7:54 AM
    Moderator
  • Hi,

    Any update?

    If the replies help you, please mark the reply as answer, it will make others who stuck with the similar issue easier to search for valid solutions in this forum.

    Best regards,
    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, October 14, 2019 9:09 AM
    Moderator
  • I need to embed the image within the email and I cannot use use urls as it is not downloaded by defauly in outlook unless it is trueted in outlook
    Monday, October 21, 2019 6:07 AM