A través de la terminal de VS Code debemos navegar a la carpeta donde esta situado el proyecto en el que queremos añadir el paquete Nuget. Una vez ahí, ejecutamos: dotnet add package Newtonsoft.Json Nota: Debéis instalar la extension de C# para VS Code. Mas información aquí: https://docs.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio-code
Etiqueta: c#
Eliminar el último caracter en un String en C#
Podemos usar la función "Remove" para eliminar al ultimo caracter de nuestro texto: myText = myText.Remove(myText.Length - 1, 1); Por ejemplo, si queremos eliminar si el ultimo caracter de nuestro texto es una coma: if(myText.EndsWith(",")) { myText = myText.Remove(myText.Length - 1, 1); }
Obtener Datos de Servicio REST con RestSharp
Este fragmento de código nos permite hacer una llamada a un Web Service REST mediante la librería RestSharp. var client = new RestClient("REST_SERVICE_URL"); client.Authenticator = new HttpBasicAuthenticator("USER", "PASSWORD"); var request = new RestRequest(Method.GET); request.RequestFormat = DataFormat.Json; IRestResponse response = client.Execute(request); var content = response.Content;
Obtener URL de la página de propiedades de un item
A través de esta función podemos obtener la URL para ver las propiedades de un item de una lista de SharePoint: public static string GetItemPropertiesURL(SPListItem item) { string web = item.Web.Url; string listID = item.ParentList.ID.ToString(); string contentType = item.ContentTypeId.ToString(); string itemID = item.ID.ToString(); string url = web + "/_layouts/listform.aspx?PageType=4&ListID={" + listID + "}&ID=" + itemID …
Sigue leyendo Obtener URL de la página de propiedades de un item
Convertir HTML en PDF en C#
Una interesante herramienta para crear facilmente PDFs desde HTML: NReco PdfGenerator. 1. Creamos nuestro HTML string html = "MY_HTML"; 2. Creamos una instancia del conversor var pdfConversor = new NReco.PdfGenerator.HtmlToPdfConverter(); 3. Configuramos las propiedades que necesitemos Modificar el grosor de los margenes pdfConversor.Margins = new NReco.PdfGenerator.PageMargins() { Bottom = 25, Left = 25, Right = 25, …
Delete SharePoint Page Programmatically
SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite("YOURSITE")) { using (SPWeb web = site.OpenWeb()) { //Obtain the page PublishingPageCollection publishingPages = PublishingWeb.GetPublishingWeb(web).GetPublishingPages(); PublishingPage page = publishingPages.FirstOrDefault(f => f.Uri.AbsoluteUri == "YOURPAGE"); if (page != null) { //Allow modifications bool webAllow = web.AllowUnsafeUpdates; web.AllowUnsafeUpdates = true; SPFile file = page.ListItem.File; //Delete file.Delete(); //Restablish default permissions web.AllowUnsafeUpdates = …
C# Error: Cannot have multiple items selected in a DropDownList
Error Cannot have multiple items selected in a DropDownList Solution This message means that you're trying to select an item and another one is already selected. To solve this problem you've to clear the previous selection an then select the new one, like this: myDropDownList.ClearSelection(); myDropDownList.Items.FindByValue(myValue).Selected = true;
Consulta SQL «SELECT» en C#
El siguiente código nos permitirá hacer una consulta a nuestra base de datos. Tan solo debemos tener una cadena de conexión (que habitualmente situaremos en el fichero de configuración de nuestra aplicación) y la consulta "SELECT" que queramos. using (SqlConnection conn = new SqlConnection(/*Our Connection String*/) { conn.Open(); using (SqlCommand cmd = new SqlCommand(/*Our SELECT …
Pasar y Obtener Parámetros de la URL
Para pasar parámetros a través de la URL nos basta con añadir un "?" al final e ir insertando los parámetros que queramos con el patrón "nombre=valor" y separados por un "&", quedando del siguiente modo: Javascript: var URL = 'myweb.aspx?param1=1¶m2=2¶m3=3'; C#: String URL = "myweb.aspx?param1=1¶m2=2¶m3=3"; Si lo que queremos es obtener estos parámetros posteriormente, …
Pasar Variables por Referencia en C#
Los parámetros que pasamos a una función los podemos pasar por valor o por referencia. El paso por valor es el que se utiliza por defecto cuando programamos en el lenguaje C#, simplemente pasamos la variable y en la definición de la función la añadimos con el tipo de variable que es. Por ejemplo: Llamada: …