Пример #1
0
        private static bool SetMeta(SharePointMetaData spModel, string contextUrl, string destinationPath)
        {
            var clientContext = new ClientContext(contextUrl);
            Web web           = clientContext.Web;
            //clientContext.Credentials = new NetworkCredential(userName, passWord, domain);
            CredentialCache cc = new CredentialCache();

            cc.Add(new Uri(webUrl),
                   "NTLM", new NetworkCredential(userName, passWord, domain));
            clientContext.Credentials        = cc;
            clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
            clientContext.Load(web);
            clientContext.ExecuteQuery();


            Microsoft.SharePoint.Client.File metaData = web.GetFileByServerRelativeUrl(destinationPath);
            //clientContext.Load(metaData.ListItemAllFields);
            //clientContext.ExecuteQuery();
            var setMetadata = metaData.ListItemAllFields;

            setMetadata["OperatingGroupCd"]       = spModel.operatingGrpCode;
            setMetadata["OperatingGroupNm"]       = spModel.operatingGrp;
            setMetadata["OperatingUnitCd"]        = spModel.operatingUnitCode;
            setMetadata["OperatingUnitNm"]        = spModel.operatingUnit;
            setMetadata["ClientServiceGroupCd"]   = spModel.clientServiceGrpCode;
            setMetadata["ClientServiceGroupDesc"] = spModel.clientServiceGrp;
            setMetadata["CustomerNm"]             = spModel.clientName;
            setMetadata["CustomerNbr"]            = spModel.clientNumber;
            setMetadata["ContractNbr"]            = spModel.contractNumber;
            setMetadata["LCUId"] = spModel.strLCUId;

            if ((spModel.moduleName == "IssuesAndRisks"))
            {
                setMetadata["ModuleNm"] = "Issues and Risks";
            }
            else
            {
                setMetadata["ModuleNm"] = spModel.moduleName;
            }

            setMetadata["ModuleId"]       = spModel.moduleId;
            setMetadata["UserId"]         = spModel.userId;
            setMetadata["FileTypeCd"]     = spModel.fileTypeCode;
            setMetadata["FileTypeNm"]     = spModel.fileType;
            setMetadata["DocumentTypeCd"] = spModel.documentTypeCode;
            setMetadata["DocumentTypeNm"] = spModel.documentType;
            setMetadata["DocumentNm"]     = spModel.documentName;
            setMetadata.Update();
            //clientContext.Load(setMetadata);
            clientContext.ExecuteQuery();
            return(true);
        }
Пример #2
0
        public static bool Move(string currentLCU,
                                string newLCU,
                                ModuleName moduleName,
                                int moduleId,
                                string geographicRegionCd,
                                string sharepointCountryCd,
                                string customerNbr,
                                string fileName,
                                string newGeographicRegionCd, string newSharepointCountryCd, string newCustomerNbr)
        {
            bool          result    = false;
            var           _region   = geographicRegionCd;
            var           _country  = sharepointCountryCd;
            var           clientNbr = customerNbr;
            string        region    = string.Empty;
            string        newRegion = string.Empty;
            StringBuilder finalUri  = new StringBuilder("");

            if (_region == "EL")
            {
                region = "EME";
            }
            if (_region == "AP")
            {
                region = "APA";
            }
            if (_region != "EL" && _region != "AP")
            {
                region = "AMR";
            }

            _region = newGeographicRegionCd;
            if (_region == "EL")
            {
                newRegion = "EME";
            }
            if (_region == "AP")
            {
                newRegion = "APA";
            }
            if (_region != "EL" && _region != "AP")
            {
                newRegion = "AMR";
            }

            if (webUrl.Substring(webUrl.Length - 1, 1) != "/")
            {
                webUrl = webUrl + "/";
            }

            try
            {
                string uri              = webUrl + region + @"/" + sharepointCountryCd;
                string newUri           = webUrl + newRegion + @"/" + newSharepointCountryCd;
                var    newClientContext = new ClientContext(newUri);
                Web    newWeb           = newClientContext.Web;
                //newClientContext.Credentials = new NetworkCredential(userName, passWord, domain);
                CredentialCache cc = new CredentialCache();
                cc.Add(new Uri(webUrl),
                       "NTLM", new NetworkCredential(userName, passWord, domain));
                newClientContext.Credentials        = cc;
                newClientContext.AuthenticationMode = ClientAuthenticationMode.Default;
                newClientContext.Load(newWeb);
                newClientContext.ExecuteQuery();
                using (ClientContext context = new ClientContext(uri))
                {
                    Web web = context.Web;
                    //context.Credentials = new NetworkCredential(userName, passWord, domain);
                    CredentialCache ccs = new CredentialCache();
                    cc.Add(new Uri(webUrl),
                           "NTLM", new NetworkCredential(userName, passWord, domain));
                    context.Credentials        = ccs;
                    context.AuthenticationMode = ClientAuthenticationMode.Default;
                    context.Load(web);
                    context.ExecuteQuery();

                    string destinationUrl = EnsureParentFolder(newLCU, moduleId.ToString(), moduleName, newGeographicRegionCd, newSharepointCountryCd, newCustomerNbr, newClientContext);

                    finalUri.Append("/sites");
                    finalUri.Append("/");
                    finalUri.Append(region);
                    finalUri.Append("/");
                    finalUri.Append(sharepointCountryCd);
                    finalUri.Append("/");
                    finalUri.Append(docLib);
                    finalUri.Append("/");
                    finalUri.Append(clientNbr);
                    finalUri.Append("/");
                    finalUri.Append(currentLCU);
                    finalUri.Append("/");
                    finalUri.Append(moduleName.ToString());
                    finalUri.Append("/");
                    finalUri.Append(moduleId);
                    finalUri.Append("/");

                    string destUrl = finalUri.ToString() + fileName;
                    Microsoft.SharePoint.Client.File loadUrl = web.GetFileByServerRelativeUrl(destUrl);
                    if (region == newRegion)
                    {
                        loadUrl.MoveTo(destinationUrl + fileName, MoveOperations.Overwrite);
                        context.ExecuteQuery();
                        result = true;
                    }
                    if (region != newRegion)
                    {
                        var setMetadata = loadUrl.ListItemAllFields;
                        context.Load(setMetadata);
                        context.ExecuteQuery();
                        SharePointMetaData spModel = new SharePointMetaData();

                        spModel.operatingGrpCode     = (string)setMetadata["OperatingGroupCd"];
                        spModel.operatingGrp         = (string)setMetadata["OperatingGroupNm"];
                        spModel.operatingUnitCode    = (string)setMetadata["OperatingUnitCd"];
                        spModel.operatingUnit        = (string)setMetadata["OperatingUnitNm"];
                        spModel.clientServiceGrpCode = (string)setMetadata["OperatingUnitNm"];
                        spModel.clientServiceGrp     = (string)setMetadata["ClientServiceGroupDesc"];
                        spModel.clientName           = (string)setMetadata["CustomerNm"];
                        spModel.clientNumber         = (string)setMetadata["CustomerNbr"];
                        spModel.contractNumber       = (string)setMetadata["ContractNbr"];
                        spModel.strLCUId             = (string)setMetadata["LCUId"];
                        spModel.moduleName           = (string)setMetadata["ModuleNm"];
                        spModel.moduleId             = (string)setMetadata["ModuleId"];
                        spModel.userId           = (string)setMetadata["UserId"];
                        spModel.fileTypeCode     = (string)setMetadata["FileTypeCd"];
                        spModel.fileType         = (string)setMetadata["FileTypeNm"];
                        spModel.documentTypeCode = (string)setMetadata["DocumentTypeCd"];
                        spModel.documentType     = (string)setMetadata["DocumentTypeNm"];
                        spModel.documentName     = (string)setMetadata["DocumentNm"];

                        string          newDestUrl = destinationUrl + fileName;
                        FileInformation loadStream = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, destUrl);

                        Microsoft.SharePoint.Client.File.SaveBinaryDirect(newClientContext, destinationUrl + fileName, loadStream.Stream, true);

                        SetMeta(spModel, newUri, newDestUrl);

                        loadUrl.DeleteObject();
                        context.ExecuteQuery();

                        result = true;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(result);
        }
Пример #3
0
        //private static NetworkCredential _credentials = new NetworkCredential(userName, passWord, domain);

        //private static ClientContext init(string contextUrl)
        //{
        //    var clientContext = new ClientContext(contextUrl);
        //    Web web = clientContext.Web;
        //    clientContext.Credentials = new NetworkCredential(userName, passWord, domain);
        //    clientContext.Load(web);
        //    clientContext.ExecuteQuery();
        //    return clientContext;
        //}

        public static bool UploadDocument(byte[] bytes, string fileName, string GeographicRegionCd, string SharepointCountryCd, string CustomerNbr, string lcuId, int moduleId, SharePointMetaData spModel, ModuleName moduleName)
        {
            bool   result  = false;
            string region  = string.Empty;
            string country = string.Empty;

            if (GeographicRegionCd == "EL")
            {
                region = "EME";
            }
            if (GeographicRegionCd == "AP")
            {
                region = "APA";
            }
            if (GeographicRegionCd != "EL" && GeographicRegionCd != "AP")
            {
                region = "AMR";
            }

            if (webUrl.Substring(webUrl.Length - 1, 1) != "/")
            {
                webUrl = webUrl + "/";
            }
            //Stream stream = new MemoryStream(bytes);

            using (var clientContext = new ClientContext(webUrl + region + @"/" + SharepointCountryCd))
            {
                string contextUrl = webUrl + region + @"/" + SharepointCountryCd;
                Web    web        = clientContext.Web;
                //clientContext.Credentials = new NetworkCredential(userName, passWord, domain);
                CredentialCache cc = new CredentialCache();
                cc.Add(new Uri(webUrl),
                       "NTLM", new NetworkCredential(userName, passWord, domain));
                clientContext.Credentials        = cc;
                clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
                clientContext.Load(web);
                clientContext.ExecuteQuery();
                string destinationUrl = EnsureParentFolder(lcuId, moduleId.ToString(), moduleName, GeographicRegionCd, SharepointCountryCd, CustomerNbr, clientContext);
                var    newFile        = web.GetFolderByServerRelativeUrl(destinationUrl);
                clientContext.Load(newFile);
                clientContext.ExecuteQuery();

                //Upload File
                using (var stream = new MemoryStream(bytes))
                {
                    clientContext.RequestTimeout = 360000;
                    string destinationPath = destinationUrl + fileName;
                    Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, destinationPath, stream, true);

                    //Set Metadata

                    Microsoft.SharePoint.Client.File metaData = web.GetFileByServerRelativeUrl(destinationPath);
                    //clientContext.Load(metaData.ListItemAllFields);
                    //clientContext.ExecuteQuery();
                    var setMetadata = metaData.ListItemAllFields;
                    setMetadata["OperatingGroupCd"]       = spModel.operatingGrpCode;
                    setMetadata["OperatingGroupNm"]       = spModel.operatingGrp;
                    setMetadata["OperatingUnitCd"]        = spModel.operatingUnitCode;
                    setMetadata["OperatingUnitNm"]        = spModel.operatingUnit;
                    setMetadata["ClientServiceGroupCd"]   = spModel.clientServiceGrpCode;
                    setMetadata["ClientServiceGroupDesc"] = spModel.clientServiceGrp;
                    setMetadata["CustomerNm"]             = spModel.clientName;
                    setMetadata["CustomerNbr"]            = spModel.clientNumber;
                    setMetadata["ContractNbr"]            = spModel.contractNumber;
                    setMetadata["LCUId"] = spModel.strLCUId;

                    if ((spModel.moduleName == "IssuesAndRisks"))
                    {
                        setMetadata["ModuleNm"] = "Issues and Risks";
                    }
                    else
                    {
                        setMetadata["ModuleNm"] = spModel.moduleName;
                    }

                    setMetadata["ModuleId"]       = spModel.moduleId;
                    setMetadata["UserId"]         = spModel.userId;
                    setMetadata["FileTypeCd"]     = spModel.fileTypeCode;
                    setMetadata["FileTypeNm"]     = spModel.fileType;
                    setMetadata["DocumentTypeCd"] = spModel.documentTypeCode;
                    setMetadata["DocumentTypeNm"] = spModel.documentType;
                    setMetadata["DocumentNm"]     = spModel.documentName;
                    setMetadata.Update();
                    clientContext.Load(setMetadata);
                    clientContext.ExecuteQuery();
                    result = true;
                }
            }
            return(result);
        }