Autor Beitrag
sworddancer
Hält's aus hier
Beiträge: 15



BeitragVerfasst: Mo 20.10.08 12:02 
Hallo zusammen,

Ich habe derzeit ein Problem mit den Webservices von Exchange 2007. Die Aufgabe ist eine Persone / Kontakt aus einem öffentlichen Ordner zu lesen. Hierführ habe ich zwei Funktionen geschrieben. Die eine Funktion gibt mir die FolderIDType des öffentlichen Ordners zurück. Die zweite funktion sucht einen kontakt aus einem Öffentlichen Ordner und liefert eine Liste zurück mit allen Kontakten die den Suchkriterien entsprechen.
Das Problem ist das die Suche in dem Öffentlichen Ordner nicht funktioniert. Zum Testen habe ich einen Öfffentlichen Ordner und meinen Normalem Kontakt Ordner mit den Identischen Daten gefüllt. Wenn ich meine Abfrage and meinen standard Kontakt Ordner schicke funktioniert es einwandfrei. Wenn ich die gleiche Abfrage an den Öffentlichen Ordner schicke bekomme ich immer einen leeren Rootfolder zurück. Vielleicht kann einer von euch mir ja weiter helfen. Würde mich auf jedenfall sehr freuen. Im folgenden poste ich noch die zwei Funktionen die ich geschrieben habe.

Die Funktion zum suchen der FolderIDType des Puplic Folders
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
public FolderIdType searchFolder(string _folderName)
        {
            //get a instanze of the exchange webservice client
            ExchangeServiceBinding server = new ExchangeServiceBinding();
            //use the current user to access the exchange
            server.Credentials = System.Net.CredentialCache.DefaultCredentials;
            //set the URL of the web service
            server.Url = "https://<ServerName>/EWS/Exchange.asmx";
            //set the function to validate the server zertifacte
            ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
            {
                return true;
            };
            //create the request and specify the traversal type
            FindFolderType findFolderRequest = new FindFolderType();
            findFolderRequest.Traversal = FolderQueryTraversalType.Shallow;

            //define the properties that the webservice should return
            FolderResponseShapeType responseShape = new FolderResponseShapeType();
            responseShape.BaseShape = DefaultShapeNamesType.AllProperties;
            findFolderRequest.FolderShape = responseShape;

            //Identify which folders to search
            DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
            folderIDArray[0] = new DistinguishedFolderIdType();
            folderIDArray[0].Id = DistinguishedFolderIdNameType.publicfoldersroot;

            //add the folders to serach to the request.
            findFolderRequest.ParentFolderIds = folderIDArray;

            //restriction tbased on the folder display name
            RestrictionType restriction = new RestrictionType();
            
            PathToUnindexedFieldType fldrRestriction = new PathToUnindexedFieldType();
            fldrRestriction.FieldURI = UnindexedFieldURIType.folderDisplayName;
            //identify the folder name to restrict one
            ContainsExpressionType contains = new ContainsExpressionType();
            contains.ContainmentMode = ContainmentModeType.FullString;
            contains.ContainmentModeSpecified = true;
            contains.ContainmentComparison = ContainmentComparisonType.IgnoreCaseAndNonSpacingCharacters;
            contains.ContainmentComparisonSpecified = true;
            contains.Item = fldrRestriction;
            contains.Constant = new ConstantValueType();
            contains.Constant.Value = _folderName;
            restriction.Item = contains;
            findFolderRequest.Restriction = restriction;

            server.RequestServerVersionValue = new RequestServerVersion();
            server.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1;

            FindFolderResponseType findFolderResponse = server.FindFolder(findFolderRequest);
            foreach (ResponseMessageType rmt in findFolderResponse.ResponseMessages.Items)
            {
                FindFolderResponseMessageType folderResponse = rmt as FindFolderResponseMessageType;
                FindFolderParentType parentFolderTyp = folderResponse.RootFolder;
                
                foreach (BaseFolderType baseResponseFolder in parentFolderTyp.Folders)
                {
                    if (baseResponseFolder is ContactsFolderType)
                    {
                        ContactsFolderType current = baseResponseFolder as ContactsFolderType;
                        return current.FolderId;
                    }
                }
            }
            return null;
        }


Die Funktion zum lesen des Kontakts
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
public IList<UserInfo> getOutlookUser(string _FirstName, string _LastName)
        {
            //get a instanze of the exchange webservice client
            ExchangeServiceBinding server = new ExchangeServiceBinding();
            //use the current user to access the exchange
            server.Credentials = System.Net.CredentialCache.DefaultCredentials;
            //set the URL of the web service
            server.Url = "https://<ServerName>/EWS/Exchange.asmx";
            //set the function to validate the server zertifacte
            ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
            {
                return true;
            };

            //create the search files
            PathToUnindexedFieldType searchfieldFirstName = new PathToUnindexedFieldType();
            searchfieldFirstName.FieldURI = UnindexedFieldURIType.contactsGivenName;

            PathToUnindexedFieldType searchfieldLastName = new PathToUnindexedFieldType();
            searchfieldLastName.FieldURI = UnindexedFieldURIType.contactsSurname;

            ConstantValueType fieldValueFirstName = new ConstantValueType();
            fieldValueFirstName.Value = _FirstName;

            ConstantValueType fieldValueLastName = new ConstantValueType();
            fieldValueLastName.Value = _LastName;

            //define the search rule for firstname
            ContainsExpressionType exprFirstName = new ContainsExpressionType();
            exprFirstName.ContainmentModeSpecified = true;
            exprFirstName.ContainmentMode = ContainmentModeType.FullString;
            exprFirstName.ContainmentComparisonSpecified = true;
            exprFirstName.ContainmentComparison = ContainmentComparisonType.IgnoreCaseAndNonSpacingCharacters;
            exprFirstName.Constant = fieldValueFirstName;
            exprFirstName.Item = searchfieldFirstName;
            
            //define the search rule for lastname
            ContainsExpressionType exprLastName = new ContainsExpressionType();
            exprLastName.ContainmentModeSpecified = true;
            exprLastName.ContainmentMode = ContainmentModeType.FullString;
            exprLastName.ContainmentComparisonSpecified = true;
            exprLastName.ContainmentComparison = ContainmentComparisonType.IgnoreCaseAndNonSpacingCharacters;
            exprLastName.Constant = fieldValueLastName;
            exprLastName.Item = searchfieldLastName;
            
            //create the and prorperty
            AndType andcondition = new AndType();
            andcondition.Items = new SearchExpressionType[2];
            andcondition.Items[0] = exprFirstName;
            andcondition.Items[1] = exprLastName;
            
            //save the search rules
            RestrictionType restriction = new RestrictionType();
            restriction.Item = andcondition;

            //set the properties that should return
            ItemResponseShapeType itemProperties = new ItemResponseShapeType();
            itemProperties.BaseShape = DefaultShapeNamesType.AllProperties;

            // Tell it you only want to look in the contacts folder
            FolderIdType[] folderIDArray = new FolderIdType[1];
            folderIDArray[0] = new FolderIdType();
            folderIDArray[0] = searchFolder(<Name des Öffentlichen Ordners>);
  
            //DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
            //folderIDArray[0] = new DistinguishedFolderIdType();
            //folderIDArray[0].Id = DistinguishedFolderIdNameType.contacts;

            //create the search class
            FindItemType findRequest = new FindItemType();
            findRequest.ItemShape = itemProperties;
            findRequest.Restriction = restriction;
            findRequest.Traversal = ItemQueryTraversalType.Shallow;
            findRequest.ParentFolderIds = folderIDArray;
            //send the search request to the webserver
            FindItemResponseType foundedItems = server.FindItem(findRequest);
            IList<UserInfo> result = new List<UserInfo>();
            if (foundedItems.ResponseMessages.Items.Length > 0)
            {
                FindItemResponseMessageType responseMessage = foundedItems.ResponseMessages.Items[0as FindItemResponseMessageType;
                if (responseMessage.RootFolder == null)
                    return result;
                ArrayOfRealItemsType realItems = responseMessage.RootFolder.Item as ArrayOfRealItemsType;
                //if ther are not items returned return a empty list
                if (!(realItems != null && realItems.Items != null))
                    return result;
                foreach (ContactItemType contact in realItems.Items)
                {
                    UserInfo info = new UserInfo();
                    info.M_Name = contact.GivenName;
                    info.M_Email = contact.EmailAddresses[0].Value;
                    info.M_Department = contact.Department;
                    info.M_City = contact.PhysicalAddresses[0].City;
                    info.M_LastName = contact.Surname;
                    info.M_MobilPhone = contact.PhoneNumbers[0].Value;
                    info.M_PLZ = contact.PhysicalAddresses[0].PostalCode;
                    info.M_Position = contact.JobTitle;
                    info.M_PrivatePhone = contact.PhoneNumbers[1].Value;
                    info.M_Street = contact.PhysicalAddresses[0].Street;
                    info.M_Superior = contact.Manager;
                    result.Add(info);
                }
                return result;
            }
            else
                return result;
        }


Ich danke eusch schonmal für eure Hilfe und verbleibe
Mit freundlichen Grüßen
sworddancer
sworddancer Threadstarter
Hält's aus hier
Beiträge: 15



BeitragVerfasst: Di 21.10.08 10:12 
Hallo zusammen,

Ich habe die Lösung jetzt selbst gefunden. Das Problem liegt daran das ich bei der Find Methode dem Server nicht gesagt habe das der Exchange Server auf Service Pack 1 läuft. Offensichtlich haben sich die ID`s der Public folder von Exchange zu Exchange SP1 in der Strucktur verändert. Deswegen kann der Webservice mit einer public folder ID von Exchange SP1 auf den Ordner nicht zugreiffen wenn der WS Client ohne SP1 läuft. Die Funktion lieert allerdings keine Fehlermeldung sondern liefert einfach eine Leere Menge zurück. Mit folgendem Code der bei jeder Erzeugung eines ExchangeServerBinding sollte deshalb folgender Code hinzugefügt werden
ausblenden C#-Quelltext
1:
2:
server.RequestServerVersionValue = new RequestServerVersion();
            server.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1;


Damit funktioniert die Abfrage nun einwandfrei. Vielleicht kann das ja jemand von euch noch gebrauchen :)

Mit freundlichen Grüßen
sworddancer