said-ipman
said-ipman

Reputation: 41

Add document sharepoint using web service Microsoft Dynamics CRM

I have an account entity in my Microsoft Dynamics CRM and the every account I have folder in Sharepoint which contains documents of this account I want to create app on c# using Web Services CRM IOrganizationService to Add Documents in SharePoint.
it's possible ?
Please any links to do that.
I need to help.
thanks in advance

Upvotes: 0

Views: 1321

Answers (1)

Dot_NET Pro
Dot_NET Pro

Reputation: 2123

from your Question what is understood is that you have setup SharePoint in CRM Document Management System. You must have enabled Document Location for Accounts in the Document Management Settings. Now you want to Create/Upload documents to the Sharepoint Library. You can use Sharepoint: Client Side Object Model(CSOM) to do that. I have a piece of code that creates documents in sharepoint:

//Main Section Code
string sharePointUrl = GetDefaultSPSiteUrlFromCRMSiteCollectionEntity();
SharePointMethods sharePointMethods = new SharePointMethods(sharePointUrl, spUsername, spPassword, spDomain);

ClientContext context = sharePointMethods._clientContext;
Web web = sharePointMethods._clientContext.Web;
FileCreationInformation newFile = new FileCreationInformation();
  newFile.Content = System.IO.File.ReadAllBytes(newTempCRFDocumentFile);
  newFile.Url = sharePointFolder.ServerRelativeUrl+ CRFfileGeneratedName;
  newFile.Overwrite = true;
  List docs = web.Lists.GetByTitle("Account");
  Microsoft.SharePoint.Client.File uploadFile = sharePointFolder.Files.Add(newFile);
  context.ExecuteQuery();

Helper Functions:

internal string GetDefaultSPSiteUrlFromCRMSiteCollectionEntity()
    {
        try
        {
            ConditionExpression c = new ConditionExpression("isdefault", ConditionOperator.Equal, true);
            FilterExpression f = new FilterExpression(LogicalOperator.And);
            f.Conditions.Add(c);

            QueryExpression q = new QueryExpression("sharepointsite");
            q.Criteria = f;
            q.ColumnSet = new ColumnSet("sharepointsiteid", "name", "absoluteurl", "relativeurl", "isdefault", "parentsite");

            EntityCollection crmSites = GRID.CRM.Common.Common.RetrieveMultiple(q);
            if (crmSites.Entities.Count > 0)
            {
                Entity defaultSharePointSite = crmSites.Entities[0];
                if (defaultSharePointSite.Attributes.Contains("parentsite") && defaultSharePointSite.Attributes.Contains("relativeurl"))
                {
                    Entity parentSiteOfDefaultSite = GRID.CRM.Common.Common.RetrieveSingle("sharepointsite", ((EntityReference)defaultSharePointSite["parentsite"]).Id);
                    return (string)parentSiteOfDefaultSite["absoluteurl"] + "/" + defaultSharePointSite.GetAttributeValue<string>("relativeurl");
                }
                else
                {
                    return defaultSharePointSite.GetAttributeValue<string>("absoluteurl");
                }
            }
            // no SharePoint Sites defined in CRM
            throw new Exception("CRM does not have any default SharePoint Sites");
        }
        catch (Exception ex)
        {
            throw new Exception("CrmMethods -> GetDefaultSPSite (" + ex.Message + ")");
        }
    }


internal class SharePointMethods
{
    string _siteUrl;
    public ClientContext _clientContext;
    internal SharePointMethods(string spSiteUrl, string spUsername, string spPassword, string spDomain)
    {
        try
        {
            _siteUrl = spSiteUrl;
            _clientContext = new ClientContext(_siteUrl);

            _clientContext.Credentials = new System.Net.NetworkCredential
                (spUsername, spPassword, spDomain);
        }
        catch (Exception ex)
        {
            throw new Exception("SharePointMethods.Constructor --> [" + ex.Message + "]");
        }
    }
}

Upvotes: 1

Related Questions