SharePoint web services: test if file exists - web-services

I'm using SharePoint web services in C#. I have my code working to check files and check them out using the Lists web service. I need to test to see if a file exists; I can find lots of examples for doing this using the object model API, but I can't seem to find a straightforward way of doing this using web services.

Try the Lists.GetListItems with a suitable CAML query.
A CAML query like
<Query><Where><Eq><FieldRef Name="FileLeafRef" /><Value Type="Text">Filename.rtf</Value></Eq></Where></Query>
should work; the field 'FileLeafRef' is where the filename is stored.

This code may do, it's a little rough, but demonstrates how to get a list of files based on the title.
public static bool PageExists(string listName, string webPath, string pageTitle)
{
string pageId = "";
IntranetLists.Lists lists = new IntranetLists.Lists();
lists.UseDefaultCredentials = true;
lists.Url = webPath + "/_vti_bin/lists.asmx";
XmlDocument doc = new XmlDocument();
doc.LoadXml("<Document><Query><Where><Contains><FieldRef Name=\"Title\" /><Value Type=\"Text\">" + pageTitle + "</Value></Contains></Where></Query><ViewFields /><QueryOptions /></Document>");
XmlNode listQuery = doc.SelectSingleNode("//Query");
XmlNode listViewFields = doc.SelectSingleNode("//ViewFields");
XmlNode listQueryOptions = doc.SelectSingleNode("//QueryOptions");
Guid g = GetWebID(webPath);
XmlNode items = lists.GetListItems(listName, string.Empty, listQuery, listViewFields, string.Empty, listQueryOptions, g.ToString());
}
return items.Count > 0;
}
public static XmlNodeList XpathQuery(XmlNode xmlToQuery, string xPathQuery)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlToQuery.OuterXml);
XmlNamespaceManager mg = new XmlNamespaceManager(doc.NameTable);
mg.AddNamespace("sp", "http://schemas.microsoft.com/sharepoint/soap/");
mg.AddNamespace("z", "#RowsetSchema");
mg.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
mg.AddNamespace("y", "http://schemas.microsoft.com/sharepoint/soap/ois");
mg.AddNamespace("w", "http://schemas.microsoft.com/WebPart/v2");
mg.AddNamespace("d", "http://schemas.microsoft.com/sharepoint/soap/directory");
return doc.SelectNodes(xPathQuery, mg);
}

I also had similiar problems with this.
I have tried the following FieldRefs without success: "Name", "FileLeafRef" and "LinkFilenameNoMenu".
The post located at http://www.johanolivier.blogspot.com details what I had to do to get it working.

Related

Web service callout failed: Unexpected element. Parser was expecting element 'http://schemas.xmlsoap.org/soap/envelope/:Envelope' but found ':HTML'

while make a webservice call out I am getting below error:
Web service callout failed: Unexpected element. Parser was expecting element 'http://schemas.xmlsoap.org/soap/envelope/:Envelope' but found ':HTML'
Please see below code which I am trying for this:
public class TestUtility_Cls{
public list<Test_webService.KeyValuePair> IOG_pair = new list<Test_webService.KeyValuePair>();
public pageReference calltestServices(){
I_pair = new list<Test_webService.KeyValuePair>();
Test_webService.webPort bindobj = new Test_Iwebervice.RtPort();
bindobj.clientCertName_x = 'xxxxxxxxxxxxxx';
bindobj.timeout_x = 120000;
bindobj.inputHttpHeaders_x = new Map<String, String>();
bindobj.inputHttpHeaders_x.put('Authorization', 'xxxxxxxxx');
Test_webService.KeyValuePair I_KeyValue = new Test_webService.KeyValuePair();
I_KeyValue.key = 'SessionId';
I_KeyValue.value = 'Carrie09';
I_pair.add(I_KeyValue);
I_KeyValue = new Test_webService.KeyValuePair();
I_KeyValue.key = 'CR';
I_KeyValue.value = 'ExOffer';
I_pair.add(I_KeyValue);
Test_webService.ArrayOfKeyValuePair kevapair = new Test_webService.ArrayOfKeyValuePair();
kevapair.attribute = I_pair;
Test_webService.ProcessEventResponse_element IResp = new Test_webService.ProcessEventResponse_element();
IResp = bindingobj.ProcessEvent('QA', 'GetOffers', kevapair);
return null;
}
}
Here I am using WSDL generated class's method.
Can someone help on this. How to resolve it?
Thanks,
public pageReference calltestServices(){
I think above method refers a html page reference from which you are extracting your input data.You are forming your input request in html format while your webservice is expectng soap envelope. I think you need to wrap or convet or edit your request, formed in above method as soap envelope, then only your server accept it.

Invoking AX web service via C#

I am trying to connect to the AX web services. What I will do it to fetch the right price given a product and a customer.
I realised the right webservice to use is PriceListServiceClient and I am able to log in to it using windows authentication, but I cannot retrieve any data from it.
Here is my code:
PriceListServiceClient priceListServiceClient = new PriceListServiceClient();
priceListServiceClient.ClientCredentials.Windows.ClientCredential.UserName = "yyy";
priceListServiceClient.ClientCredentials.Windows.ClientCredential.Password = "zzz!";
priceListServiceClient.ClientCredentials.Windows.ClientCredential.Domain = "xxx";
CriteriaElement[] criteriaElement = new CriteriaElement[1];
criteriaElement[0] = new CriteriaElement();
criteriaElement[0].DataSourceName = "SalesPrice";
criteriaElement[0].FieldName = "ItemId";
criteriaElement[0].Operator = Operator.NotEqual;
criteriaElement[0].Value1 = "5637153175";
QueryCriteria queryCriteria = new QueryCriteria();
queryCriteria.CriteriaElement = criteriaElement;
CallContext callContext = new CallContext();
var found = priceListServiceClient.find(callContext, queryCriteria);
Console.WriteLine(found.Currency);
priceListServiceClient.Close();
Any idea about why this is happening?
Try filling in the properties in the CallContext (company and language).
new CallContext { Company = "zzz", Language = "nl" };
I found the answer here: http://community.dynamics.com/ax/f/33/p/118741/246784.aspx
The Ax class for Price List document is AxPriceDiscTmpPrintout Class. This class wraps the TmpPriceDiscPrintout table, which is a TMP table. That's why you are not getting anything in return.

How to specify language, location and city with facebook graph api

I am using FacebookGraphAPIDesktop.swc api to create unpublished post.
How can I assign language, location and city parameter with it?
Currently I am creating it like below :-
var params:Object = new Object();
params.access_token = PhotoToken;
params.published = 0;
params.message = "Test unpublished post";
FacebookDesktop.api("/" + PageID +"/feed", StatusPosted, params,"POST");
thanks in advance
I found the solution and posting here if anyone needs it :-
We need to pass it as a json string :-
var targeting:String = "{'countries':['US','IN']}";
_params.access_token = token;
_params.published = 0;
_params.message = "This is status message 5";
_params.targeting = targeting;
FacebookDesktop.api("/202387076493582/feed", messagePosted1, _params,"POST");

How to populate XML node values in Grid view in Asp.net?

I am using a thirdparty webservice. I got the response in XML format.
Now, i have to show the XML node values in grid view.
The following code i tried so far.
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
//Label2.Text = reader.ReadToEnd();
XmlDocument xml = new XmlDocument();
xml.Load(reader);
XmlNamespaceManager ns = new XmlNamespaceManager(xml.NameTable);
ns.AddNamespace("ms", "http://webservices.amazon.com/AWSECommerceService/2005-10-05");
XmlNode image = xml.SelectSingleNode("//ms:URL", ns);
XmlNode FormattedPrice = xml.SelectSingleNode("//ms:FormattedPrice", ns);
Now, i want to show the values of XMLnode value in grid view.
Please inform me, if you need more information.
Thanks in advance.
You can use an XMLDataSourcefor that:
http://www.codeproject.com/Articles/10898/Introduction-to-XMLDataSource-control-in-ASP-NET-2
Best regards.
Solution:
Instead of Get the HttpResponse in xmldocument.
I used XNamespace and XDocument.
XNamespace ns = "http://webservices.amazon.com/AWSECommerceService/2005-10-05"; // Linq
XDocument xd = XDocument.Load(response.GetResponseStream());
Then Use Linq to Read all Vlaues. Example:
var Image = xd.Descendants(ns + "Items").Elements(ns + "Item").Select(img => img.Elements(ns + "MediumImage").Select(img1 => (string)img1.Element(ns + "URL")).FirstOrDefault() ?? "Null").ToList();

Why is this CAML Query is not filtering results from SharePoint Web Service call?

I am trying to filter the list of results returned by the following web service call to a SharePoint list. When I have
query.InnerText = "";
the results include every row in the SharePoint list - including rows having an IN SERVICE DATE less than today, greater than today, or no value at all. I need the CAML query to filter, but adding the Where statement does not work.
service.Url = url;
String pipeProjectSummaryListGuid = "{2D193799-F1FB-46B1-A313-56B8B12E1111}";
XmlDocument xmlDoc = new System.Xml.XmlDocument();
XmlElement query = xmlDoc.CreateElement("Query");
XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
String rowLimit = "9000";
query.InnerText = "<Where><Gt><FieldRef Name=\"In_x0020_Service_x0020_Date\" /><Value Type=\"DateTime\"><Today /></Value></Gt></Where>";
viewFields.InnerXml = "<FieldRef Name=\"NEW_ID\" /><FieldRef Name=\"In_x0020_Service_x0020_Date\" /><FieldRef Name=\"ProjectPriority\" />";
queryOptions.InnerXml = "<ViewAttributes Scope=\"Recursive\" /><IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>";
try
{
results = service.GetListItems(pipeProjectSummaryListGuid, null, query, viewFields, rowLimit, queryOptions, null);
}
Try using query.InnerXml instead of query.InnerText.