Search This Blog

Tuesday, May 21, 2019

Get Sum/Aggregate from Related entity using c# in Dynamics CRM

public int GetTotal()
{
FetchExpression f1 = new FetchExpression("<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true' aggregate='true' >" +
                    "    <entity name='account' >" +
                    "        <filter type='and' >" +
                    "            <condition attribute='statuscode' operator='in' >" +
                    "                <value>" +
                    "                    1" +
                    "                </value>" +
                    "                <value>" +
                    "                    175650000" +
                    "                </value>" +
                    "            </condition>" +
                    "        </filter>" +
                    "        <link-entity name='batch' from='batchid' to='batchid' visible='false' link-type='outer' alias='bc' >" +
                    "            <attribute name='credithour' aggregate='sum' alias='totalcredithours' />" +
                    "        </link-entity>" +
                    "    </entity>" +
                    "</fetch>");

EntityCollection AccountCollection = service.RetrieveMultiple(f1);
                int total = 0;
                if (AccountCollection != null)
                {
                    AccountCount = AccountCollection.Entities.Count;
                    if (AccountCount > 0)
                    {
                        foreach (var entity in AccountCollection.Entities)
                        {
                            if (entity.Attributes.Contains("totalcredithours"))
                            {
                                total = Convert.ToInt16(((AliasedValue)entity.Attributes["totalcredithours"]).Value);
                            }
                            return total;
                        }
                    }
                }
}


Monday, May 20, 2019

Insert in List Item in Shairpoint online using c#

 protected void InsertItem()
        {
            try
            {
                SecureString Password = new SecureString();
                Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

                foreach (char c in "password")
                {
                    Password.AppendChar(c);
                }
                    ClientContext clientcontext = new ClientContext(url);//"https://orgname.sharepoint.com/sites/MainListSite"
                    clientcontext.Credentials = new SharePointOnlineCredentials(username, Password);

                    List olist = clientcontext.Web.Lists.GetByTitle("FirstMainList");
                    ListItemCreationInformation listiteminfor = new ListItemCreationInformation();
                    Microsoft.SharePoint.Client.ListItem olistitem = olist.AddItem(listiteminfor);

                    olistitem["Title"] = "1";
              
                    olistitem.Update();
                    clientcontext.ExecuteQuery();

            }
            catch(Exception ex)
            {
                Label1.Text = ex.Message;
            }
        }

Retrieve List Items form Shairpoint Online in c#

protected void GetListItem()
        {
            try
            {
                SecureString Password = new SecureString();
                Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

                foreach (char c in "password")
                {
                    Password.AppendChar(c);
                }
                using (ClientContext context = new ClientContext(url))//https://orgname.sharepoint.com/sites/MainListSite"
                {
                    context.Credentials = new SharePointOnlineCredentials(username, Password);

                    List list = context.Web.Lists.GetByTitle("FirstMainList");//list name
                    context.Load(list);
                    context.ExecuteQuery();
                    Microsoft.SharePoint.Client.View view = list.Views.GetByTitle("All Items");//view name

                    context.Load(view);
                    context.ExecuteQuery();
                    CamlQuery query = new CamlQuery();
                    query.ViewXml = view.ViewQuery;

                    Microsoft.SharePoint.Client.ListItemCollection items = list.GetItems(query);
                    context.Load(items);
                    context.ExecuteQuery();
                    //Console.WriteLine("Total Count: " + items.Count);

                    foreach (Microsoft.SharePoint.Client.ListItem itm in items)
                    {
                        Label1.Text = itm["Title"].ToString() + itm["Mobile"].ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                Label1.Text = "Exception " + ex.Message;
            }
        }