вторник, 31 января 2012 г.

Копирование файла из файловой системы в библиотеку SharePoint


using (SPSite oSite = new SPSite(http://siteUrl))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                    SPList oList = oWeb.Lists["libraryName"];
                    string fileName = "fileName"; //Имя файла
                    string Path = "C:\folder\\";  //Путь к файлу
                    FileStream fileStream = null;

                    try
                    {
                        fileStream = File.OpenRead(Path + fileName);
                        Byte[] fileContent = new byte[Convert.ToInt32(fileStream.Length)];
                        fileStream.Read(fileContent, 0, Convert.ToInt32(fileStream.Length));
                        oList.RootFolder.Files.Add(oList.RootFolder.Url + "/" + fileName, fileContent, true);
                        oList.Update();
                    }
                    catch (Exception ex)
                    {
                        //Вывод сообщения об ошибки
                    }
                    finally
                    {
                        if (fileStream != null)
                        {
                            fileStream.Close();
                        }
                    }
                }
            }

понедельник, 30 января 2012 г.

Получение Руководителя через UserProfile

SPSite oSite = new SPSite("nameSite");
SPWeb oWeb = oSite.OpenWeb();
SPUser oUser = oWeb.CurrentUs er();
SPServiceContext oSPServiceContext = SPServiceContext.GetContext(oSite);
UserProfileManager oUserProfileManager = new UserProfileManager(oSPServiceContext);
UserProfile oUserProfile = oUserProfileManager.GetUserProfile(oUser.ID);

UserProfile[] users = oUserProfile.GetManagers();
SPUser Manager =
oWeb.AllUsers[oUserProfile[PropertyConstants.Manager].ToString()];

//Теперь в Manager хранится руководитель oUser

Создание папки в библиотеке и добавление в нее файла из FileUpload

if (FileUpload1.HasFile != false)
                {
                    byte[] contents = GetFileContents(FileUpload1.PostedFile);

                    if (contents != null)
                    {
                        String url =
                        web.Lists[nameListDoc].RootFolder.ServerRelativeUrl.ToString();
                        SPFolderCollection oFolders = web.GetFolder(url).SubFolders;
                        oFolders.Add(nameNewFolder);
                        string folderString = nameListDoc+ "/" + nameNewFolder;
                        SPFolder oFolder = web.GetFolder(folderString);
                        SPFileCollection oFiles = oFolder.Files;
                        oFiles.Add(FileUpload1.FileName, contents);
                    }
                }

private byte[] GetFileContents(HttpPostedFile postedFile)
        {
            HttpPostedFile file = postedFile;
            Stream oStream = file.InputStream;
            byte[] contents = new byte[oStream.Length];
            oStream.Read(contents, 0, (int)oStream.Length);
            oStream.Close();
            oStream.Dispose();
            return contents;
        }

SPQuery и поле Date

В списке есть поле Дата(только дата, без времени) нужно было сделать Query запрос чтобы выбрать все элементы с датой СЕГОДНЯ + 3 ДНЯ.

SPSite oSite = new SPSite("nameSite")
SPWeb oWeb = oSite.OpenWeb()
SPList oListHoliDays = oWeb.Lists["nameList"];
SPQuery oQuery = new SPQuery();
DateTime oDateTime = DateTime.Now.AddDays(3);
DateTime newDate = DateTime.Parse(oDateTime.ToShortDateString());
oQuery.Query = "<Where>"+
                   "<Eq>" +
                      "<FieldRef Name='Date' />"+
                        "<Value IncludeTimeValue='TRUE' Type='DateTime'>" +
                     SPUtility.CreateISO8601DateTimeFromSystemDateTime(newDate)+
                        "</Value>" +
                   "</Eq>" +
                "</Where>";
SPListItemCollection oListItemColl = oListHoliDays.GetItems(oQuery);

воскресенье, 29 января 2012 г.

Отправить письмо с вложением

            string smtpServer = SPAdministrationWebApplication.Local.
            OutboundMailServiceInstance.Server.Address;
            string smtpFrom = SPAdministrationWebApplication.
            Local.OutboundMailSenderAddress;

            MailAddress fromAdress = new MailAddress(from);
            MailAddress toAdress = new MailAddress(to);
            MailMessage mailMessage = new MailMessage(fromAdress, toAdress);

            mailMessage.Subject = title;

            mailMessage.Body = message;
            mailMessage.IsBodyHtml = true;
            WebClient oWebClient = new WebClient();
            oWebClient.Credentials = CredentialCache.DefaultNetworkCredentials;
            string attachment_url = "http://sitename/biblio/file.docx";
            byte[] data = oWebClient.DownloadData(attachment_url);
            MemoryStream oMemoryStream = new MemoryStream(data);
            string filename_attachment = "filename.docx";
            mailMessage.Attachments.Add(new System.Net.Mail.
                Attachment(oMemoryStream, filename_attachment, MediaTypeNames.Text.Plain));
            SmtpClient oSmtpClient = new SmtpClient(smtpServer);
            oSmtpClient.Send(mailMessage);

Если необходимо отправить письмо с пометкой важно, то мы просто добавляем:
mailMessage.Priority = MailPriority.High;