Can be get desired column from share point list? - vb.net

I am using the following Caml Query to get data from a share point list.
oSb.Append(" <OrderBy>")
oSb.Append(" <FieldRef Name=""Title"" />")
oSb.Append(" </OrderBy>")
But this query is giving me all columns of list.
Is there any way to get only desired column from a share point list using Caml Query?
Please help me.

Take a look at the MSDN article about View Fields. You can specify those fields before running your CAML query.
using (SPSite site = new SPSite("http://localhost"))
{
using (SPWeb web = site.OpenWeb())
{
// Build a query.
SPQuery query = new SPQuery();
query.Query = string.Concat(
"<Where><Eq>",
"<FieldRef Name='Status'/>",
"<Value Type='CHOICE'>Not Started</Value>",
"</Eq></Where>",
"<OrderBy>",
"<FieldRef Name='DueDate' Ascending='TRUE' />",
"<FieldRef Name=’Priority’ Ascending='TRUE' />",
"</OrderBy>");
query.ViewFields = string.Concat(
"<FieldRef Name='AssignedTo' />",
"<FieldRef Name='LinkTitle' />",
"<FieldRef Name='DueDate' />",
"<FieldRef Name='Priority' />");
query.ViewFieldsOnly = true; // Fetch only the data that we need.
// Get data from a list.
string listUrl = web.ServerRelativeUrl + "/lists/tasks";
SPList list = web.GetList(listUrl);
SPListItemCollection items = list.GetItems(query);
// Print a report header.
Console.WriteLine("{0,-25} {1,-20} {2,-25} {3}",
"Assigned To", "Task", "Due Date", "Priority");
// Print the details.
foreach (SPListItem item in items)
{
Console.WriteLine("{0,-25} {1,-20} {2,-25} {3}",
item["AssignedTo"], item["LinkTitle"], item["DueDate"], item["Priority"]);
}
}

Related

Prestashop 1.7 module : Need to get "Country" and "State" names, "Products" and "Order message"

I am developing a module, it gets order details when order statut is changed to "shipped" by admin, to post them to a third party application with api. I am using Prestashop V 1.7.7.0.
I still need to get "country" and "state" names, "products" and "order message". How can i do that please?
Also, the module can't be installed in prestashop. Is my code right?
Need help please. Thanks
public function hookActionOrderStatusPostUpdate($params)
{
if($params['newOrderStatus']->id == 13)
{
$order = new Order((int)$params['id_order']);
$address = new Address((int)$order->id_address_delivery);
$customer = new Customer((int)($address->id_customer));
$country = new Country((int)($address->id_country));
$state = new Country((int)($address->id_state));
$tel_cl = $address->phone;
$name_lastname_cl = $address->lastname . ' ' . $address->firstname;
$country_cl = **Not yet**;
$state_cl = **Not yet**;
$adress_cl = $address->address1 . ' ' . $address->address2;
$tel_2_cl = $address->phone_mobile
$products = **Not yet**;
$quantity = "1"
$cod = $order->total_paid
$note = **Not yet(order message)**;
$Url_str = 'http://example.com/api/set_parcel_post.php?id=123&tel_cl='.$tel_cl.'&name_lastname_cl='.$name_lastname_cl.'&country_cl='.$country_cl.'&state_cl='.$state_cl.'&address_cl='.$address_cl.'&tel_2_cl='.$tel_2_cl.'&products='.$products.'&cod='.$cod.'&Quantity='.$Quantity.'&note='.$note;
$json = file_get_contents($Url_str);
$result = json_decode($json);
}
}
You can't instantiate a Country object using an id_state so this:
$state = new Country((int)($address->id_state));
is wrong and should be
$state = new State((int) $address->id_state);
So you can get the state name like $state->name,
To get the product orders you can use $order->getProducts(); which will return an array filled with the order products and its information(name,price,etc..)

CRM 2013 refresh subgrid with fetchxml

In my scenario, i have a new entity called "new_relations" with 2 fields (account1 and account2) that are both lookups to accounts.
In my accounts form i want to display a grid containing any record with the current account, in any of the 2 lookups, that exists in "new_relations" entity.
I´ve created account A and account B; created 1 record in new_relations with new_relations.account1 = account A and new_relations.account2 = account B.
So, when i open account A or Account B i want to see the record created in new_relations.
I have the following code, unfortunately its only showing the record in Account´s A form in my subgrid...
Can anyone help?
function FilterRelacao(){
var relacoes = document.getElementById("Relacoes");
var account = Xrm.Page.data.entity.getId();
var accountname = Xrm.Page.data.entity.attributes.get("name").getValue();
if(relacoes==null){
setTimeout(function () { FilterRelacao(); }, 2000);
return;
}
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='new_relations'>" +
"<attribute name='new_type' />" +
"<attribute name='new_accountid1' />" +
"<attribute name='new_accountid2' />" +
"<attribute name='new_relationsid' />" +
"<order attribute='new_accountid1' descending='true' />" +
"<filter type='and'>" +
"<condition attribute='statecode' operator='eq' value='0' />" +
"<filter type='or'>" +
"<condition attribute='new_accountid1' operator='eq' uitype='account' uiname='" + accountname+ "' value='" + account + "' />" +
"<condition attribute='new_accountid2' operator='eq' uitype='account' uiname='" + accountname + "' value='" + account + "' />" +
"</filter></filter>" +
"</entity>" +
"</fetch>";
relacoes.control.SetParameter("fetchXml", fetchXml); //set the fetch xml to the sub grid
relacoes.control.SetParameter("effectiveFetchXml", fetchXml); //set the fetch xml to the sub grid
relacoes.control.Refresh(); //refresh the sub grid using the new fetch xml
}
Your fetch XML looks correct to me. When you set up the subgrid did you set "Records" = "All Record Types" in the Data Source section? If you did not then the subgrid will append a condition to your fetchxml so that it returns only records related to the specific relationship that you specified.

pass an ID with hyperlik but cant get this ID value from a fk in one table when i click in insert

Something strange happened in my codes, actually I have a hyperlink that pass ID value in a query string to second page.in second page i have 2 sql datasource that both these sql datasources should get this id value and pass it to a filter parameter to show sth in datalist.
so in another word I have a first page that has an hyperlink read ID value from a datasource and pass it to second page.its like below:
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "~/forumpage.aspx?ID="+Eval("ID")%>'><%#Eval("title")%> </asp:HyperLink>
then in second page i have one sql datasource with a query like this ...where ID=#id and get this id in query string from db.it work great . but i have problem with second sql datasource in second page it has a query sth like below:...forms.question_id=#id
then in sql reference both to query string as ID that get by first page in hyperlink.
but when i click in insert button show me error with fk.
error:Error:The INSERT statement conflicted with the FOREIGN KEY
constraint "FK_forumreply_forumquestions". The conflict occurred in
database "forum", table "dbo.forumquestions", column 'ID'. The
statement has been terminated.
my tables
(question(ID,user_id(fk),Cat_id(fk),title,bodytext)
(reply(ID,userr_id(fk),questionn_id(fk),titlereply,bodytestreply);
When by hand in cb i gave a number in questionn_id like 1 it show me successful but when it want read from a filter by datasource this field face with problem.
plzzzz help i really need skip from this part.and cause i am new i guess I cant understand the logic way clearly.
<asp:SqlDataSource ID="sdsreply" runat="server"
ConnectionString="<%$ ConnectionStrings:forumConnectionString %>"
SelectCommand="SELECT forumreply.ID, forumreply.userr_id, forumreply.questionn_id, forumreply.bodytextreply, forumreply.datetimereply, forumquestions.ID AS Expr1, forumusers.ID AS Expr2, forumusers.username FROM forumquestions INNER JOIN forumreply ON forumquestions.ID = forumreply.questionn_id INNER JOIN forumusers ON forumquestions.user_id = forumusers.ID AND forumreply.userr_id = forumusers.ID where forumreply.questionn_id=#questionn_id">
<SelectParameters>
<asp:QueryStringParameter Name="questionn_id" QueryStringField="ID" />
</SelectParameters>
</asp:SqlDataSource>
it is cb for second page in insert button:
{
if (Session["userid"] != null)
{
lblreply.Text = Session["userid"].ToString();
}
else
{
Session["userid"]=null;
}
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
lblshow.Text = string.Empty;
string d = HttpContext.Current.User.Identity.Name;
lblshow.Text =d + "عزیز خوش آمدید." ;
foreach (DataListItem item in DataList2.Items)
{
Label questionn_idLabel = (Label)item.FindControl("questionn_idLabel");
Label userr_idLabel = (Label)item.FindControl("userr_idLabel");
lbltest.Text = string.Empty;
lbltest.Text = questionn_idLabel.Text;
lblreply.Text = string.Empty;
lblreply.Text = userr_idLabel.Text;
}
}
else
{
lblshow.Text = "اگر بار اول هست که می خواهید پاسخ دهید لطفا ابتدا ثبت نام و سپس لاگین فرمائید.";
}
}
{
if(HttpContext.Current.User.Identity.IsAuthenticated)
{
if (Page.IsValid)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["forumConnectionString"].ConnectionString);
try
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into forumreply (userr_id,questionn_id,bodytextreply,datetimereply)values(#userr_id,#questionn_id,#bodytextreply,#datetimereply)", con);
cmd.Parameters.AddWithValue("userr_id",lblreply.Text);
cmd.Parameters.AddWithValue("questionn_id",lbltest.Text);
cmd.Parameters.AddWithValue("bodytextreply",txtbody.Text);
cmd.Parameters.AddWithValue("datetimereply",DateTime.Now );
cmd.ExecuteNonQuery();
}
catch (Exception exp)
{
Response.Write("<b>Error:</b>");
Response.Write(exp.Message);
}
finally
{
con.Close();
}
lblmsg.Text = "پیام شما با موفقیت ثبت گردید.thx";
lblshow.Visible = false;
//lbltxt.Text = txtbody.Text;
txtbody.Text = string.Empty;
}
}
else
{
lblmsg.Text = string.Empty;
Session["rem"] = Request.UrlReferrer.AbsoluteUri;
Response.Redirect("~/login.aspx");
}
}

Issue in making appear all database table field names

I have this code which i use in order to make appear all the names of a table of a database.
It used to work... but suddenly it won't make appear anything..
Could you please take a look?
I'm working with SQL.
$section = "SELECT * FROM forma";
$res = odbc_exec($connection, $section) or die(odbc_error());
$firstrow = false;
while ($row = odbc_fetch_array($res)){
if (!$firstrow) {
foreach ($row as $column => $value) {
echo "<label> " . $column . "</label>";
echo "<input type='checkbox' name='data[]' value='" . $column . "' /><br/><br/>";
}
$firstrow = true;
}
}
Thanks
This is a fairly nasty way of retrieving the column names for a table. What it is doing is reading all the data in the table, ignoring the result set and only using it for the column names. What is happening however is that the table is empty and so nothing is being returned at all.
You need to amend the query to look at the meta data rather than the table itself. You will need to rework it. This SQL will retrieve the column names for that table for you...
Select Columns.Name
From Sys.Columns
Where Object_Name(Columns.Object_id) = 'forma'
Order By Columns.Column_Id;
After that you will need to rejig your code to take advantage of it.

CAML Query using "Membership Type='CurrentUserGroups'" returns no results

I have the following CAML query to programmatically filter a list:
<Query>
<Where>
<And>
<Geq><FieldRef Name='notificationExpires' /><Value IncludeTimeValue='TRUE' Type='DateTime'>2011-03-30T00:00:00Z</Value></Geq>
<Or>
<Membership Type='CurrentUserGroups'><FieldRef Name='notificationTargetRoles'/></Membership>
<Contains><FieldRef Name='notificationTargetRoles'/><Value Type='User'>MyDomain\administrator</Value></Contains>
</Or>
</And>
</Where>
</Query>
When I execute this exact same query (running under the same account) in the U2U CAML Query Builder I get filtered users, groups and dates.
When apply exact the same filter in code, the Groups do not appear in the results.
What can be the reason that the group filter (CurrentGroups) does not return any results?
The code for applying the filter is:
SPListItemCollection items = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedSite = new SPSite(theSiteName))
{
using (SPWeb elevatedWeb = elevatedSite.OpenWeb())
{
SPList alertList = elevatedWeb.Lists[theListName];
SPQuery query = new SPQuery();
query.Query = "<Where><And><Geq><FieldRef Name='notificationExpires' /><Value IncludeTimeValue='TRUE' Type='DateTime'>2011-03-30T00:00:00Z</Value></Geq> <Or><Membership Type='CurrentUserGroups'><FieldRef Name='notificationTargetRoles'/></Membership><Contains><FieldRef Name='notificationTargetRoles'/><Value Type='User'>BSFLMK\administrator</Value></Contains></Or></And></Where>";
items = alertList.GetItems(query);
}
}
});
It turns out that removing the "SPSecurity.RunWithElevatedPrivileges(delegate()" part corrected the problem