How do I upload an image from my gallery to a sharepoint library ?

I want my application to fetch the image from gallery and upload the same image to the Sharepoint library.

I googled for a solution but unable to find one that I could understand.
Anyone who has implemented the same or has a sample with them.
Do reply

Answers

  • @sameerpurohit said:
    I want my application to fetch the image from gallery and upload the same image to the Sharepoint library.

    I googled for a solution but unable to find one that I could understand.
    Anyone who has implemented the same or has a sample with them.
    Do reply

    looking solution for the same.

  • AjaysinhDodiyaAjaysinhDodiya INMember ✭✭
    • use AzureActiveDirectoryAuthenticator and get the accessToken

    • get images from gallery
      using MessagingCenter.Subscribe for pick image from gallery

    • upload Images
      C# using (var client = new HttpClient()) { var SourceContent=ImagebyteArrayContent; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", acessToken); using (var response = client.PostAsync(new Uri("your tenant.sharepoin.com" + "/_api/web/GetFolderByServerRelativeUrl('/PublishingImages')/Files/add(url='" + filename + "',overwrite=true)"), new StreamContent(new MemoryStream(SourceContent))).Result) { if (response.IsSuccessStatusCode) { success = true; } } }

  • AjaysinhDodiyaAjaysinhDodiya INMember ✭✭

    @AjaysinhDodiya said:

    • use AzureActiveDirectoryAuthenticator and get the accessToken

    • get images from gallery
      using MessagingCenter.Subscribe for pick image from gallery

    • upload Images
      C# using (var client = new HttpClient()) { var SourceContent=ImagebyteArrayContent; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", acessToken); using (var response = client.PostAsync(new Uri("your tenant.sharepoin.com" + "/_api/web/GetFolderByServerRelativeUrl('/PublishingImages')/Files/add(url='" + filename + "',overwrite=true)"), new StreamContent(new MemoryStream(SourceContent))).Result) { if (response.IsSuccessStatusCode) { success = true; } } }

  • BrunoPasquini.7813BrunoPasquini.7813 AGMember ✭✭

    @AjaysinhDodiya can you provide more information about how to use AzureActiveDirectoryAuthenticator to get the accessToken?

  • AjaysinhDodiyaAjaysinhDodiya INMember ✭✭
    edited March 23

    Hello @BrunoPasquini.7813 Try Following Code for get AccessToken

    install Microsoft.IdentityModel.Clients.ActiveDirectory in android and ios and create dependencyService

    pcl

    public interface IAzureActiveDirectoryAuthenticator
    {
        Task<string> Authenticate(string authority, string resource, string clientId, string returnUri);
        Task LogOut(string authority);
    }
    

    for Android

    class DroidAzureADAL : IAzureActiveDirectoryAuthenticator
    {
        public async Task<string> Authenticate(string authority, string resource, string clientId, string returnUri)
        {
            var authContext = new AuthenticationContext(authority);
            if (authContext.TokenCache.ReadItems().Any())
                authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
    
            var uri = new Uri(returnUri);
            var platformParams = new PlatformParameters((Activity)Forms.Context);
            var authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams);
    
            var accessToken = authResult.AccessToken;
            return accessToken;
        }
    
        public Task LogOut(string authority)
        {
            var authContext = new AuthenticationContext(authority);
            authContext.TokenCache.Clear();
            return Task.CompletedTask;
        }
    }
    

    for iOS

     public class IosAzureADAL : IAzureActiveDirectoryAuthenticator
    {
        public async Task<string> Authenticate(string authority, string resource, string clientId, string returnUri)
        {
            var authContext = new AuthenticationContext(authority);
            if (authContext.TokenCache.ReadItems().Any())
                authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
    
            var controller = UIApplication.SharedApplication.KeyWindow.RootViewController;
            var uri = new Uri(returnUri);
            var platformParams = new PlatformParameters(controller);
            var authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams);
    
            var accessToken = authResult.AccessToken;
            return accessToken;
        }
    
        public Task LogOut(string authority)
        {
            throw new NotImplementedException();
        }
    }
    
Sign In or Register to comment.