0

AutodeskWebDev - 博客园

 3 years ago
source link: https://www.cnblogs.com/seanchen/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
AutodeskWebDev - 博客园

为一个演讲大会做了一个list供用户上传照片(实现了固定图片大小和格式的限制), 通过sharepoint网站可以直接查看所有演讲者(speaker)的照片。用户又希望可以把所有人的照片一并下载到本地,并以演讲者的姓名命名。查了一下msdn,为这个功能写了个小工具,代码很简单,通过lists.asmx提供的web service来实现,大体如下:

using (SharePoint_WS_Lists.Lists listWS = new SharePoint_WS_Lists.Lists())
            {
                listWS.Url = http://server/sites/mysite/_vti_bin/lists.asmx;
                listWS.Credentials = System.Net.CredentialCache.DefaultCredentials;
                listWS.UseDefaultCredentials = true;

                XmlDocument xmlDoc = new System.Xml.XmlDocument();
                XmlNode ndListItems = null;
                XmlNamespaceManager nsmgr;
                XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
                XmlNode ndViewFields =
                    xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
                XmlNode ndQueryOptions =
                    xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");

                ndQueryOptions.InnerXml =
                    "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>" +
                    "<DateInUtc>TRUE</DateInUtc>";
                ndViewFields.InnerXml = "<FieldRef Name='Speaker' /><FieldRef Name='ID'/>";
                ndQuery.InnerXml = "<Where><Gt><FieldRef Name='ID'/>" +
                    "<Value Type='Number'>0</Value></Gt></Where>";
                try
                {
                    ndListItems =
                        listWS.GetListItems("Photo Upload", null, ndQuery,
                        ndViewFields, null, ndQueryOptions, null);
                    //MessageBox.Show(ndListItems.OuterXml);

                }
                catch (System.Web.Services.Protocols.SoapException ex)
                {
                    MessageBox.Show("Message:\n" + ex.Message + "\nDetail:\n" +
                        ex.Detail.InnerText +
                         "\nStackTrace:\n" + ex.StackTrace);
                }
                nsmgr = new XmlNamespaceManager(ndListItems.OwnerDocument.NameTable);
                nsmgr.AddNamespace("s", "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882");
                nsmgr.AddNamespace("dt", "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882");
                nsmgr.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
                nsmgr.AddNamespace("z", "#RowsetSchema");



                string speaker;
                int iCount = 0;
                string sTest = "";
         
                foreach (XmlNode xmlNode in ndListItems.SelectNodes("rs:data/z:row", nsmgr))
                {
                    iCount++;

                    try
                    {
                        speaker = xmlNode.Attributes["ows_Speaker"].Value;
                        int strLastIndx = speaker.LastIndexOf(@";#");

                        speaker = speaker.Substring(strLastIndx + 2);

                        XmlNode node = listWS.GetAttachmentCollection("Photo Upload", xmlNode.Attributes["ows_ID"].Value);
                        DataSet ds = new DataSet();
                        using (XmlNodeReader reader = new XmlNodeReader(node))
                        {
                            ds.ReadXml(reader);
                        }

                        DataTable dtAttachment = ds.Tables[0];

                        for (int iCnt = 0; iCnt <= dtAttachment.Rows.Count - 1; iCnt++)
                        {
                            string sourceUrl = Convert.ToString(dtAttachment.Rows[iCnt][0]);
                            if (sourceUrl.ToLower().IndexOf(".jpg") > 0)
                            {
                            }
                            else
                            {
                                //int test = iCnt;///root/Datasets/Dataset[contains(@Pattern, 'xyz')]
                                continue;
                            }

                            //strLastIndx = sourceUrl.LastIndexOf(@"/");
                            //string FileName = sourceUrl.Substring(strLastIndx + 1);

                            using (WebClient client = new WebClient())
                            {
                                client.UseDefaultCredentials = true;
                                byte[] response = client.DownloadData(sourceUrl);

                                FileStream fStream = new FileStream(@"C:\share\photo\" + speaker + ".jpg", FileMode.Create, FileAccess.ReadWrite);
                                fStream.Write(response, 0, response.Length);
                                fStream.Close();

                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        string sEx = ex.Message;

                        continue;
                    }
                }
            }  
         }  

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK