/// <summary> /// <c>UploadItemUsingCopyService</c> member function /// it calls <c>SPCopyServiceClass.UploadItemUsingCopyService</c> function to uploads the file to mapped sharepoint 2007 document library /// </summary> /// <param name="uploadData"></param> /// <param name="property"></param> /// <returns></returns> public static bool UploadItemUsingCopyService(UploadItemsData uploadData, CommonProperties property) { bool result = false; try { // for 2007 result = SPCopyServiceClass.UploadItemUsingCopyService(uploadData, property); //for 2010 } catch (Exception ex) { result = false; } return result; }
/// <summary> /// <c>UploadItemUsingCopyService</c> member function /// this uploads the file to sharepoint document library /// </summary> /// <param name="uploadData"></param> /// <param name="property"></param> /// <returns></returns> public static bool UploadItemUsingCopyService(UploadItemsData uploadData, CommonProperties property) { bool result = false; try { SPCopyService.Copy copyws = new Copy(); byte[] fileBytes = null; string[] destinationUrls = null; //Replace "" with empty uploadData.UploadFileName = uploadData.UploadFileName.Replace("\"", " "); if (uploadData.UploadType == TypeOfUploading.Mail) { //Get mail item string tempFilePath = UserLogManagerUtility.RootDirectory + @"\\temp.msg"; if (Directory.Exists(UserLogManagerUtility.RootDirectory) == false) { Directory.CreateDirectory(UserLogManagerUtility.RootDirectory); } Outlook.MailItem omail = uploadData.UploadingMailItem; omail.SaveAs(tempFilePath, Outlook.OlSaveAsType.olMSG); //// load the file into a file stream //Read data to byte fileBytes = File.ReadAllBytes(tempFilePath); uploadData.UploadFileName = uploadData.UploadFileName.Trim() + uploadData.UploadFileExtension; } else { //Set fullname to filename fileBytes = uploadData.AttachmentData; uploadData.UploadFileName = uploadData.UploadFileName.Trim() + uploadData.UploadFileExtension; } // format the destination URL destinationUrls = new string[] { property.LibSite + property.UploadDocLibraryName + "/" + uploadData.UploadFileName }; // to specify the content type FieldInformation ctInformation = new FieldInformation(); ctInformation.DisplayName = "Content Type"; ctInformation.InternalName = "ContentType"; ctInformation.Type = FieldType.Choice; ctInformation.Value = "Your content type"; //FieldInformation[] metadata = { titleInformation }; FieldInformation[] metadata = { }; // execute the CopyIntoItems method copyws.CopyIntoItemsAsync("OutLook", destinationUrls, metadata, fileBytes, uploadData); } catch (Exception) { result = false; } return result; }
/// /////////////////////////////////////////////////////////////// /// ///////////////Modified code by Joy on 20.07.2012////////////// /// /////////////////////////////////////////////////////////////// /// <summary> /// <c>GetListItemsAsyncWrapper</c> wrapper function /// assign properties to <c>ListWebService.Lists</c> class object /// and Register <c>GetListItemsCompleted</c> event of object /// it finally calls <c> listService.GetListItemsAsyn</c> function to sync uploading file /// </summary> /// <param name="m_uploadDocLibraryName"></param> /// <param name="viewname"></param> /// <param name="ndQuery"></param> /// <param name="ndViewFields"></param> /// <param name="rowlimit"></param> /// <param name="ndQueryOptions"></param> /// <param name="webid"></param> /// <param name="uploadData"></param> /// <param name="property"></param> private void GetListItemsAsyncWrapper(string m_uploadDocLibraryName, string viewname, XmlNode ndQuery, XmlNode ndViewFields, string rowlimit, XmlNode ndQueryOptions, string webid, object uploadData, CommonProperties property) { try { // for 2007// ListWebService.Lists listService = new ListWebService.Lists(); listService.Credentials = property.Credentionals; listService.Url = property.CopyServiceURL; listService.GetListItemsCompleted += new ListWebService.GetListItemsCompletedEventHandler(listService_GetListItemsCompleted); // listService.GetListItemsAsync(m_uploadDocLibraryName, null, ndQuery, ndViewFields, "2", ndQueryOptions, null, uploadData); XmlNode n = listService.GetListCollection(); string urlroot = property.LibSite.Remove(property.LibSite.Length - 1); foreach (XmlNode item in n.ChildNodes) { string strcompare = urlroot + item.Attributes["DefaultViewUrl"].Value; if (strcompare == property.CompletedoclibraryURL) { m_uploadDocLibraryName = item.Attributes["Title"].Value; cmproperties.UploadDocLibraryName = m_uploadDocLibraryName; break; } } listService.GetListItemsAsync(m_uploadDocLibraryName, null, ndQuery, ndViewFields, "2", ndQueryOptions, null, uploadData); } catch (Exception ex) { } }
/// <summary> /// <c>ShowForm</c> member functrion /// retrieve the connection properties form configuration file and assign them to member fields /// required to upload files to sharepoint mapped document library. /// </summary> /// <param name="folderName"></param> public void ShowForm(string folderName) { try { //Add trust security for SSL enable sites System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy(); m_isUploadingCompleted = false; HideProgressPanel(m_isUploadingCompleted); //code written by Joy] //invoke is used to avoid error-cross thread operation not valid this.Invoke(new MethodInvoker(delegate { lblPleaseWaitMessage.Text = "Please Wait - Uploading Items"; })); this.Refresh(); //Create instance for the API // copyws = new SPCopyService.Copy(); // listService = new SharePoint_Link.ListWebService.Lists(); lstwebclass = new ListWebClass(); cmproperties = new CommonProperties(); ThisAddIn.IsUploadingFormIsOpen = true; //get the folder details from xml file XmlNode uploadFolderNode = UserLogManagerUtility.GetSPSiteURLDetails("", folderName); if (uploadFolderNode != null) { this.dgvUploadImages.Rows.Clear(); m_uploadFolderNode = uploadFolderNode; cmproperties.UploadFolderNode = uploadFolderNode; cmproperties.CompletedoclibraryURL = uploadFolderNode.ChildNodes[4].InnerText; m_sharepointLibraryURL = uploadFolderNode.ChildNodes[4].InnerText; m_userName = EncodingAndDecoding.Base64Decode(uploadFolderNode.ChildNodes[0].InnerText); m_password = EncodingAndDecoding.Base64Decode(uploadFolderNode.ChildNodes[1].InnerText); m_uploadDocLibraryName = uploadFolderNode.ChildNodes[3].InnerText; ; m_AuthenticationType = uploadFolderNode.ChildNodes[5].InnerText; try { m_spSiteVersion = uploadFolderNode.ChildNodes[12].InnerText; SPVersionClass.SPSiteVersion = m_spSiteVersion; } catch (Exception) { SPVersionClass.SPSiteVersion = SPVersionClass.SiteVersion.SP2007.ToString(); } ///////// //////////////// // copyws.CopyIntoItemsCompleted += new CopyIntoItemsCompletedEventHandler(copyws_CopyIntoItemsCompleted); // listService.GetListItemsCompleted += new SharePoint_Link.ListWebService.GetListItemsCompletedEventHandler(listService_GetListItemsCompleted); if (m_AuthenticationType == "Domain Credentials") { //set windows domain credentionals to service System.Net.ICredentials credentionals = System.Net.CredentialCache.DefaultCredentials; // copyws.Credentials = credentionals; // listService.Credentials = credentionals; cmproperties.Credentionals = credentionals; } else { //Pass username/pwd to network credentionals System.Net.NetworkCredential credentionals = new System.Net.NetworkCredential(m_userName, m_password); // copyws.Credentials = credentionals; // listService.Credentials = credentionals; cmproperties.Credentionals = credentionals; cmproperties.UserName = m_userName; cmproperties.Password = m_password; } m_uploadDocLibraryName = m_uploadFolderNode.ChildNodes[3].InnerText; //Get site URL if (m_sharepointLibraryURL.Contains(m_uploadDocLibraryName) == true) { m_LibSite = m_sharepointLibraryURL.Substring(0, m_sharepointLibraryURL.IndexOf(m_uploadDocLibraryName)); //Assign URL to services // copyws.Url = m_LibSite + @"_vti_bin/Copy.asmx"; // listService.Url = m_LibSite + @"_vti_bin/Lists.asmx"; cmproperties.CopyServiceURL = m_LibSite + @"_vti_bin/Lists.asmx"; //this.ParentForm.Opacity = 0.8; this.Show(); this.Refresh(); cmproperties.UploadDocLibraryName = m_uploadDocLibraryName; cmproperties.LibSite = m_LibSite; bool result = lstwebclass.GetSPListName(cmproperties); // GetSPListName(); ////////////SP2010 written by mushtaq ahmad//// //CommonProperties cmnprop = new CommonProperties(); //cmnprop.UploadDocLibraryName = m_uploadDocLibraryName + "sip"; //cmnprop.SharepointLibraryURL = m_LibSite; //bool test = Utility.ClientObjectClass.GetSPListName(cmnprop); /////////////////////////End of SP2010///////////////////////// if (result == false) { // this.Close(); } } ////////////// ///////////// } else { EncodingAndDecoding.ShowMessageBox("ShowForm", "Unable to find folder", MessageBoxIcon.Error); } } catch (Exception ex) { if (ex.Message.Contains("401: Unauthorized") == true) { string outMessage = ex.Message + "\r\nYou may get this error if the provided credentails to the folder are wrong."; EncodingAndDecoding.ShowMessageBox("ShowForm", outMessage, MessageBoxIcon.Error); } else { EncodingAndDecoding.ShowMessageBox("ShowForm", ex.Message, MessageBoxIcon.Error); } } }
/// <summary> /// <c>UploadItemUsingCopyService</c> /// Upload Items using copy web service API /// calls <c>SPCopyClass.UploadItemUsingCopyService(</c> method to start uploading file to sharepoint 2007 mappped sharepoint /// document library. /// </summary> /// <param name="uploadData"></param> private void UploadItemUsingCopyService(UploadItemsData uploadData) { try { System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy(); CommonProperties comproperties = new CommonProperties(); comproperties.LibSite = m_LibSite; comproperties.UploadDocLibraryName = m_uploadDocLibraryName; m_isUploadingCompleted = false; //oldcode// //byte[] fileBytes = null; //string[] destinationUrls = null; ////Replace "" with empty //uploadData.UploadFileName = uploadData.UploadFileName.Replace("\"", " "); //if (uploadData.UploadType == TypeOfUploading.Mail) //{ // //Get mail item // string tempFilePath = UserLogManagerUtility.RootDirectory + @"\\temp.msg"; // if (Directory.Exists(UserLogManagerUtility.RootDirectory) == false) // { // Directory.CreateDirectory(UserLogManagerUtility.RootDirectory); // } // Outlook.MailItem omail = uploadData.UploadingMailItem; // omail.SaveAs(tempFilePath, Outlook.OlSaveAsType.olMSG); // //// load the file into a file stream // //Read data to byte // fileBytes = File.ReadAllBytes(tempFilePath); // uploadData.UploadFileName = uploadData.UploadFileName.Trim() + uploadData.UploadFileExtension; //} //else //{ // //Set fullname to filename // fileBytes = uploadData.AttachmentData; // uploadData.UploadFileName = uploadData.UploadFileName.Trim() + uploadData.UploadFileExtension; //} //// format the destination URL //destinationUrls = new string[] { m_LibSite + m_uploadDocLibraryName + "/" + uploadData.UploadFileName }; ////destinationUrls = new string[] { m_sharepointLibraryURL.Substring(0,m_sharepointLibraryURL.LastIndexOf("Forms")-1) + "/" + uploadData.UploadFileName }; //// to specify the content type //FieldInformation ctInformation = new FieldInformation(); //ctInformation.DisplayName = "Content Type"; //ctInformation.InternalName = "ContentType"; //ctInformation.Type = FieldType.Choice; //ctInformation.Value = "Your content type"; ////FieldInformation[] metadata = { titleInformation }; //FieldInformation[] metadata = { }; //// execute the CopyIntoItems method //copyws.CopyIntoItemsAsync("OutLook", destinationUrls, metadata, fileBytes, uploadData); //// End of oldcode// if (SPCopyClass.UploadItemUsingCopyService(uploadData, comproperties)) { sendingRequestCount++; } } catch (Exception ex) { if (ex.Message.Contains("401: Unauthorized") == true) { string outMessage = ex.Message + "\r\nYou may get this error if the provided credentails to the folder are wrong."; EncodingAndDecoding.ShowMessageBox("UploadCopyCalling", outMessage, MessageBoxIcon.Information); } else { EncodingAndDecoding.ShowMessageBox("UploadCopyCalling", ex.Message, MessageBoxIcon.Error); } } }
/// <summary> /// <c>getFileID_SecondAlternate</c> member function /// gets ID of the file uploaded to sharepoint document library when <c>getFileIDAlternate</c> member function throw exception /// </summary> /// <param name="udataitem"></param> /// <param name="cmproperty"></param> /// <param name="siteurl"></param> /// <param name="docLibrary"></param> /// <returns></returns> private static string getFileID_SecondAlternate(SharePoint_Link.UserModule.UploadItemsData udataitem, CommonProperties cmproperty, string siteurl, string docLibrary) { string result = string.Empty; try { Microsoft.SharePoint.Client.ClientContext ct = new ClientContext(siteurl); ct.Credentials = ListWebClass.GetCredential(cmproperty); result = UpdateAttributes(udataitem, result, ct, cmproperty, docLibrary); ct.Dispose(); } catch (Exception) { result = getFileIDByIteration(udataitem, cmproperty, siteurl, docLibrary); } return result; }
/// <summary> /// <c>getFileIDByIteration</c> member function /// gets ID of the file uploaded to sharepoint document library when <c>getFileID_SecondAlternate</c> member function throw exception /// </summary> /// <param name="udataitem"></param> /// <param name="cmproperty"></param> /// <param name="siteurl"></param> /// <param name="docLibrary"></param> /// <returns></returns> private static string getFileIDByIteration(SharePoint_Link.UserModule.UploadItemsData udataitem, CommonProperties cmproperty, string siteurl, string docLibrary) { string result = string.Empty; Microsoft.SharePoint.Client.ClientContext ct = new ClientContext(siteurl); try { ct.Credentials = ListWebClass.GetCredential(cmproperty); Web oWeb = ct.Web; ct.Load(oWeb); ct.ExecuteQuery(); ListCollection currentListCollection = oWeb.Lists; ct.Load(currentListCollection); ct.ExecuteQuery(); int found = 0; foreach (List oList in currentListCollection) { string one = docLibrary.Trim(); string two = oList.Title.Trim(); int indexofspace = -1; indexofspace = one.IndexOf(' '); while (indexofspace != -1) { one = one.Remove(indexofspace, 1); indexofspace = one.IndexOf(' '); } indexofspace = two.IndexOf(' '); while (indexofspace != -1) { two = two.Remove(indexofspace, 1); indexofspace = two.IndexOf(' '); } if (one == two) { List list2 = ct.Web.Lists.GetByTitle(oList.Title); result = UpdateAttributes(udataitem, result, ct, cmproperty, docLibrary); //old removed found += 1; } if (found > 0) { break; } } } catch (Exception) { try { result = UpdateAttributes(udataitem, result, ct, cmproperty, docLibrary); } catch { } } ct.Dispose(); return result; }
/// <summary> /// <c>GetFileID</c> member function /// this member function is used to get ID of the file recently uploaded to sharepoint document library /// </summary> /// <param name="udataitem"></param> /// <param name="cmproperty"></param> /// <param name="leftpart"></param> /// <returns></returns> private static string GetFileID(SharePoint_Link.UserModule.UploadItemsData udataitem, CommonProperties cmproperty, string leftpart) { string test = cmproperty.SharepointLibraryURL; string result = string.Empty; try { Microsoft.SharePoint.Client.ClientContext ct = new ClientContext(cmproperty.LibSite); ct.Credentials = ListWebClass.GetCredential(cmproperty); result = UpdateAttributes(udataitem, result, ct, cmproperty, cmproperty.UploadDocLibraryName); ct.Dispose(); } catch (Exception ex) { try { result = getFileIDByIteration(udataitem, cmproperty, cmproperty.LibSite, cmproperty.UploadDocLibraryName); if (string.IsNullOrEmpty(result)) { //////////////// if (leftpart.StartsWith("/")) { leftpart = leftpart.Remove(0, 1); } string temurl = cmproperty.LibSite + leftpart; result = getFileIDAlternate(udataitem, cmproperty, temurl); /////////////////// if (string.IsNullOrEmpty(result)) { result = getFileID_SecondAlternate(udataitem, cmproperty, cmproperty.LibSite, leftpart + cmproperty.UploadDocLibraryName); } ///////////////// if (string.IsNullOrEmpty(result)) { string p = leftpart; string[] split = p.Split('/'); int len = split.Length; for (int i = len - 1; i > 0; i--) { int ind = p.LastIndexOf(split[i].ToString()); if (ind != -1) { p = p.Remove(ind); temurl = cmproperty.LibSite + p; result = getFileIDAlternate(udataitem, cmproperty, temurl); if (!string.IsNullOrEmpty(result)) { break; } } } if (string.IsNullOrEmpty(result)) { for (int i = 1; i < len - 1; i++) { int ind = p.LastIndexOf(split[i].ToString()); if (ind != -1) { p = p.Remove(ind); temurl = cmproperty.LibSite + p; result = getFileID_SecondAlternate(udataitem, cmproperty, cmproperty.LibSite, p + cmproperty.UploadDocLibraryName); getFileIDAlternate(udataitem, cmproperty, temurl); if (!string.IsNullOrEmpty(result)) { break; } } } } } } ////////////////// } catch (Exception) { } } return result; }
/// <summary> /// <c>GetSPListNameUsingService</c> member function /// Retrieve the Document Library Nmae from sharepoint 2007 /// </summary> /// <param name="property"></param> /// <returns></returns> public Boolean GetSPListNameUsingService(CommonProperties property) { XmlNode listResponse = null; try { //Get the list details by its name listResponse = listService.GetList(property.UploadDocLibraryName); } catch (Exception ex) { property.UploadDocLibraryName = string.Empty; if (ex.Message.Contains("HTTP status 401: Unauthorized.")) { return false; } else { try { XmlNode allLists = listService.GetListCollection(); XmlDocument allListsDoc = new XmlDocument(); allListsDoc.LoadXml(allLists.OuterXml); XmlNamespaceManager ns = new XmlNamespaceManager(allListsDoc.NameTable); ns.AddNamespace("d", allLists.NamespaceURI); XmlNodeList xlist = allListsDoc.SelectNodes("/d:Lists/d:List[starts-with(@DefaultViewUrl,'" + property.UploadFolderNode.ChildNodes[11].InnerText + "')]", ns); if (xlist.Count > 0) { string viewURL = xlist[0].Attributes["DefaultViewUrl"].Value; if (viewURL.StartsWith(property.UploadFolderNode.ChildNodes[11].InnerText)) { property.UploadDocLibraryName = xlist[0].Attributes["Title"].Value; //m_uploadLibNameFromAllLists = xlist[0].Attributes["Title"].Value; } } } catch (Exception ex1) { } } } if (string.IsNullOrEmpty(property.UploadDocLibraryName)) { return false; } return true; }
/// <summary> /// <c>GetSPListName</c> member function /// returns the name of sharepoint List by calling <c>GetSPListNameUsingService</c> member function for sharepoint 2007 /// or <c>GetSPListNameUsingClientOM</c> member function for sharepoit 2010 /// </summary> /// <param name="property"></param> /// <returns></returns> public Boolean GetSPListName(CommonProperties property) { bool result = false; try { if (SPVersionClass.SPSiteVersion == SPVersionClass.SiteVersion.SP2007.ToString()) { //using service for 2007 result = GetSPListNameUsingService(property); } else { if (SPVersionClass.SPSiteVersion == SPVersionClass.SiteVersion.SP2010.ToString()) { //using clien for 2010 result = GetSPListNameUsingClientOM(property); } else { } } } catch (Exception) { result = false; } return result; }
/// <summary> /// <c>GetListItemsAsync</c> Event Handler /// assigns properties to <c>ListWebService.Lists</c> object. /// it register <c>GetListItemsCompleted</c> Event of <c>ListWebService.Lists</c> object /// and calls <c>GetListItemsAsync</c> function to upload files to sharepoint document library in sharepoint 2007 /// </summary> /// <param name="m_uploadDocLibraryName"></param> /// <param name="viewname"></param> /// <param name="ndQuery"></param> /// <param name="ndViewFields"></param> /// <param name="rowlimit"></param> /// <param name="ndQueryOptions"></param> /// <param name="webid"></param> /// <param name="uploadData"></param> /// <param name="property"></param> public void GetListItemsAsync(string m_uploadDocLibraryName, string viewname, XmlNode ndQuery, XmlNode ndViewFields, string rowlimit, XmlNode ndQueryOptions, string webid, object uploadData, CommonProperties property) { try { if (SPVersionClass.SPSiteVersion == SPVersionClass.SiteVersion.SP2007.ToString()) { // for 2007// ListWebService.Lists listService = new ListWebService.Lists(); listService.Credentials = property.Credentionals; listService.Url = property.CopyServiceURL; listService.GetListItemsCompleted += new ListWebService.GetListItemsCompletedEventHandler(listService_GetListItemsCompleted); listService.GetListItemsAsync(m_uploadDocLibraryName, null, ndQuery, ndViewFields, "2", ndQueryOptions, null, uploadData); } } catch (Exception) { } }
/// <summary> /// <c>uploadFilestoLibraryUsingClientOM</c> member function /// upload file to sharepoint Document Library using Client object model /// </summary> /// <param name="udataitem"></param> /// <param name="cmproperty"></param> /// <returns></returns> public static bool uploadFilestoLibraryUsingClientOM(SharePoint_Link.UserModule.UploadItemsData udataitem, CommonProperties cmproperty) { bool result = false; try { CommonProperties cmproperties = new CommonProperties(); string tempFilePath = UserLogManagerUtility.RootDirectory + "\\" + udataitem.UploadFileName;// +udataitem.UploadFileExtension; if (System.IO.File.Exists(tempFilePath)) { System.IO.File.Delete(tempFilePath); } FileStream fs = new FileStream(tempFilePath, FileMode.Create, FileAccess.ReadWrite); //////// out look storage////////// try { if (udataitem.UploadType == TypeOfUploading.Mail) { Stream msgstream = new MemoryStream(cmproperty.FileBytes); Utility.OutlookStorage.Message message = new OutlookStorage.Message(msgstream); if (!string.IsNullOrEmpty(message.BodyText)) { // HashingClass.Hashedemailbody = HashingClass.ComputeHashWithoutSalt(message.BodyText, "MD5"); } if (!string.IsNullOrEmpty(message.Subject)) { HashingClass.Mailsubject = message.Subject; } if (message.SentDate != "") { HashingClass.Modifieddate = message.SentDate; } } } catch (Exception) { } //////////End of outlook storage//// if (cmproperty.FileBytes != null) { BinaryWriter bw = new BinaryWriter(fs); bw.Write(cmproperty.FileBytes); bw.Close(); } ///updated new 6jan////// string strsitepath = cmproperty.LibSite; string strleftpart = "/"; int firstindex = -1; int secondindex = -1; firstindex = strsitepath.IndexOf(@"//"); try { if (firstindex != -1) { if (strsitepath.Length >= (firstindex + 3)) { secondindex = strsitepath.IndexOf(@"/", firstindex + 3); if (secondindex != -1) { if (strsitepath.Length >= (secondindex + 1)) { strleftpart = strleftpart + strsitepath.Substring(secondindex + 1); if (strsitepath.Length > (secondindex + 1)) { strsitepath = strsitepath.Remove(secondindex + 1); } cmproperty.LibSite = strsitepath; } } } } } catch (Exception) { } string pth = strleftpart + cmproperty.UploadDocLibraryName + "/" + udataitem.UploadFileName; // end of new/// Microsoft.SharePoint.Client.ClientContext ct = new ClientContext(cmproperty.LibSite); ct.Credentials = ListWebClass.GetCredential(cmproperty); using (FileStream filestream = new FileStream(tempFilePath, FileMode.Open)) { Microsoft.SharePoint.Client.File.SaveBinaryDirect(ct, pth, filestream, true); }; result = true; ///// // Get ID of Uploaded File FileID = GetFileID(udataitem, cmproperty, strleftpart); HashingClass.Mailsubject = ""; HashingClass.Modifieddate = ""; HashingClass.Hashedemailbody = ""; //// //////////// ///////////// if (System.IO.File.Exists(tempFilePath)) { System.IO.File.Delete(tempFilePath); } } catch (Exception ex) { string errmessage = ex.Message.ToString(); SharePoint_Link.frmUploadItemsList.UpLoadErrorMessage = errmessage;// ex.InnerException.Message.ToString(); result = false; } return result; }
/// <summary> /// <c>GetSPListNameUsingClientOM</c> member function /// gets the Sharepoint document library name for sharepoint 2010 /// </summary> /// <param name="property"></param> /// <returns></returns> public static Boolean GetSPListNameUsingClientOM(CommonProperties property) { Boolean result = false; try { //Get the list details by its name ClientContext clientcontext = new ClientContext(property.LibSite); List mylist = clientcontext.Web.Lists.GetByTitle(property.UploadDocLibraryName); try { clientcontext.Credentials = property.Credentionals; clientcontext.Load(clientcontext.Web); clientcontext.Load(mylist); clientcontext.ExecuteQuery(); result = true; clientcontext.Dispose(); } catch (Exception) { try { clientcontext.Credentials = new System.Net.NetworkCredential(property.UserName, property.Password); // property.Credentionals; clientcontext.Load(clientcontext.Web); clientcontext.Load(mylist); clientcontext.ExecuteQuery(); result = true; clientcontext.Dispose(); } catch (Exception) { string strdomainanduser = property.UserName; int indx = strdomainanduser.LastIndexOf("\\"); if (indx != -1) { string domain = strdomainanduser.Substring(0, indx); string uname = strdomainanduser.Substring(indx + 1); clientcontext.Credentials = new System.Net.NetworkCredential(uname, property.Password, domain); clientcontext.Load(clientcontext.Web); clientcontext.Load(mylist); clientcontext.ExecuteQuery(); result = true; clientcontext.Dispose(); } } } } catch (Exception ex) { result = false; } return result; }
/// <summary> /// <c>GetCredential</c> member function /// gets the user credentials based on configuration settings and returns ICredentials /// </summary> /// <param name="cProp"></param> /// <returns></returns> public static System.Net.ICredentials GetCredential(CommonProperties cProp) { System.Net.ICredentials result = null; try { //Get the list details by its name ClientContext clientcontext = new ClientContext(cProp.LibSite); List mylist = clientcontext.Web.Lists.GetByTitle(cProp.UploadDocLibraryName); try { result = cProp.Credentionals; clientcontext.Credentials = result; clientcontext.Load(clientcontext.Web); clientcontext.Load(mylist); clientcontext.ExecuteQuery(); result = cProp.Credentionals; clientcontext.Dispose(); } catch (Exception) { try { clientcontext.Credentials = new System.Net.NetworkCredential(cProp.UserName, cProp.Password); // property.Credentionals; clientcontext.Load(clientcontext.Web); clientcontext.Load(mylist); clientcontext.ExecuteQuery(); result = new System.Net.NetworkCredential(cProp.UserName, cProp.Password); clientcontext.Dispose(); } catch (Exception) { string strdomainanduser = cProp.UserName; int indx = strdomainanduser.LastIndexOf("\\"); if (indx != -1) { string domain = strdomainanduser.Substring(0, indx); string uname = strdomainanduser.Substring(indx + 1); try { clientcontext.Credentials = new System.Net.NetworkCredential(uname, cProp.Password, domain); clientcontext.Load(clientcontext.Web); clientcontext.Load(mylist); clientcontext.ExecuteQuery(); result = new System.Net.NetworkCredential(uname, cProp.Password, domain); clientcontext.Dispose(); } catch (Exception) { result = cProp.Credentionals; } } } } } catch (Exception ex) { } return result; }
/// <summary> /// <c>UpdateAttributes</c> member function /// this member function updates metatags in sharepoint document library /// </summary> /// <param name="udataitem"></param> /// <param name="result"></param> /// <param name="ct"></param> /// <param name="cmproperty"></param> /// <param name="UploadDocLibraryName"></param> /// <returns></returns> private static string UpdateAttributes(SharePoint_Link.UserModule.UploadItemsData udataitem, string result, Microsoft.SharePoint.Client.ClientContext ct, CommonProperties cmproperty, string UploadDocLibraryName) { List list; string doclibtitle = UploadDocLibraryName; try { list = ct.Web.Lists.GetByTitle(UploadDocLibraryName); ct.Load(list); ct.ExecuteQuery(); } catch (Exception ex) { ListCollection lc = ct.Web.Lists; ct.Load(lc); ct.ExecuteQuery(); foreach (List item in lc) { string siteurlroot = cmproperty.LibSite.Remove(cmproperty.LibSite.Length - 1); string url = siteurlroot + item.DefaultViewUrl; if (url == cmproperty.CompletedoclibraryURL) { doclibtitle = item.Title; break; } } } list = ct.Web.Lists.GetByTitle(doclibtitle); CamlQuery camlQuery = new CamlQuery(); camlQuery.ViewXml = @"<View> <Query> <Where> <Eq> <FieldRef Name='FileLeafRef' /> <Value Type='Text' >" + udataitem.UploadFileName + @" </Value> </Eq> </Where> </Query> <RowLimit>100</RowLimit> </View>"; ListItemCollection listItems = list.GetItems(camlQuery); ct.Load(listItems); ct.ExecuteQuery(); foreach (ListItem l in listItems) { result = l["ID"].ToString(); try { l["Title"] = HashingClass.Mailsubject; l.Update(); ct.ExecuteQuery(); l["ModifiedDate"] = HashingClass.Modifieddate; l.Update(); ct.ExecuteQuery(); } catch (Exception ex) { } } return result; }