Add document sharepoint using web service Microsoft Dynamics CRM

偶尔善良 提交于 2019-12-13 09:46:50

问题


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


回答1:


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 + "]");
        }
    }
}


来源:https://stackoverflow.com/questions/35405178/add-document-sharepoint-using-web-service-microsoft-dynamics-crm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!