Home RSS 2.0 ATOM 1.0  CDF  
 
CodeSegment - Carlos Segura Sanz (blog)
 
Page 1 of 1 in the csegScript category

Lo primero advierto que esta practica no esta del todo recomendada. Pero seguro que en más de una ocasión os habéis encontrado con el problema que supone tener el campo Título pre-configurado por SharePoint, (por lo menos deja cambiarle el nombre) pero no deja de ser una limitación en muchos casos que el tipo predeterminado sea un campo alfanumérico de 255 caracteres.

La definición en CAML del campo Título es la siguiente:

<Field Type="Text" DisplayName="Título" MaxLength="255" Name="Title" ColName="nvarchar1" />

Algunas veces he editado este campo usando CAML y he cambiado el tipo (yo por el momento no he tenido problemas al hacerlo pero como he dicho al principio es una practica que no se aconseja). Hoy trasteando he recordado que alguien (no recuerdo quien si no lo pondría) había realizado un webpart para resetear el campo Título,  de manera que después se le pudiera cambiar el tipo. De modo que he echado mano de mi webpart csegScript, y en unos minutos he dado con el código.

imports Microsoft.SharePoint 
imports Microsoft.SharePoint.Utilities 
imports Microsoft.SharePoint.WebPartPages 
imports Microsoft.SharePoint.WebControls 
imports System.Web.UI 
imports System 
 
Module Script 
Public Sub Main() 
 
Dim site As SPSite = SPControl.GetContextSite(System.Web.HttpContext.Current) 
Dim allSites As SPWebCollection = site.AllWebs 
Dim subSite As SPWeb = allSites(0) 
 
if (Param1<>"") and (Param2<>"") then 
 
Dim list as SPList = subSite.Lists(Param1) 
Dim field As SPField = list.Fields(Param2) 
 
output.WriteLine("<br>Current SchemaXml:" + SPEncode.HtmlEncode(field.SchemaXml)) 
 
Dim schemaXML As String = "<Field Type=""Text""  DisplayName=""" & field.Title & 
                 """ MaxLength=""255"" Name=""" & field.InternalName  & """ ColName=""nvarchar2"" />" 
 
output.WriteLine("<pre>" & SPEncode.HtmlEncode(schemaXML) & "</pre>") 
output.flush() 
 
try 
 
field.SchemaXml = schemaXML 
'field.Update() 
output.WriteLine("<font color=red>Ok</font>") 
 
catch ex As Exception 
 
output.WriteLine("<font color=red>Error</font>") 
output.WriteLine(ex.Message) 
 
end try 
 
output.WriteLine("<table width='100%'><tr><td>Name</td><td>Description</td><td>InternalName</td><td>Type</td></tr>") 
 
output.WriteLine("<tr><td>"+
	SPEncode.HtmlEncode(field.Title)+"</td><td>"+
        SPEncode.HtmlEncode(field.Description)+"</td><td>"+
        SPEncode.HtmlEncode(field.InternalName)+"</td><td>"+
        SPEncode.HtmlEncode(field.TypeAsString)+"</td></tr>") 
 
output.WriteLine("</table>") 
 
End If 
 
End Sub 
End Module

Este script requiere el nombre de la lista como Param1 y el nombre del campo a resetear como Param2.

Después de ejecutar el script y entrar a modificar la configuración de la lista y el campo, podeís cambiarle el tipo.

 

Tuesday, January 17, 2006 10:27:17 PM (Hora estándar romance, UTC+01:00)   #    Comments [0]   csegScript | SharePoint-es  | 

I want to do a recopilation the csegScripts to make list template with all. If you want I can add yours scripts, please send it to me.   

Quero hacer una recopilacin con los scripts que usas con csegScript, y hacer una plantilla con todos ellos, si quieres que aada los tuyos, enviamelos.

Tuesday, October 18, 2005 12:27:16 AM (Hora de verano romance, UTC+02:00)   #    Comments [0]   csegScript  | 

This weekend I have finished the first version of csegScript, now are two webparts in the package, the first webpart is the 0.99 version with some improvements in the code, the  operation basically is the same one. (See previous post)

The second new webpart uses a list to store the scripts, and has three slots to pass parameters to the webpart.  (Thanks to Stramit by your idea)

The list fields are:

Title: Script Title
References: Dll necessary to execute the code (remember that are Server based paths)
Code: The script code
Description: Short description of the code
Param1, Param2, Param3: Default values for parameters
Param1Desc, Param2Desc, Param3Desc: Parameters descriptions

If you uses the list you dont need add the double slash at the end of each line of code, now you can write the code in a list text field using cr-lf.

To referer the params in the code use Param1, Param2 and Param3.

Hello World Sample

Running the webpart


When you runs the csegScript webpart you can view a combo box with all scripts that you have stored in the list and only needs select the one that you want to execute, you can view the script description and the parameters slots with your descriptions. If you changes the parameters values press apply to re-run the script. Also there is a link to the list to modify the script.

Attached in the zip file is the list template.

 csegScriptWebPart1.zip (23,37 KB) 

Monday, September 05, 2005 2:11:15 PM (Hora de verano romance, UTC+02:00)   #    Comments [1]   csegScript  | 

Some users of csegRollUp2 ask to me how to get the complete list of fields for a list, remember that you can use the internal names with csegRollUp. Now you can use csegScript to show all fields and types of any list.

Change the red code with the name of your list, and put the webpart in the site where the list is stored. 

Begin Script Code

   1:imports Microsoft.SharePoint
   2:imports Microsoft.SharePoint.Utilities
   3:imports Microsoft.SharePoint.WebPartPages
   4:imports Microsoft.SharePoint.WebControls
   5:imports System.Web.UI
   6:Module Script
   7:Public Sub Main()
   8:Dim site As SPSite = SPControl.GetContextSite(System.Web.HttpContext.Current)
   9:Dim allSites As SPWebCollection = site.AllWebs
  10:Dim subSite As SPWeb = allSites(0)
  11:Dim list as SPList = subSite.Lists("Anuncios")
  12:Dim field As SPField
  13:output.WriteLine("<table width='100%'><tr><td>Name</td><td>Description</td><td>InternalName</td><td>Type</td></tr>")
  14:For Each field in list.Fields
  15:output.WriteLine("<tr><td>"+SPEncode.HtmlEncode(field.Title)+"</td><td>"+SPEncode.HtmlEncode(field.Description)+"</td><td>"+SPEncode.HtmlEncode(field.InternalName)+"</td><td>"+SPEncode.HtmlEncode(field.TypeAsString)+"</td></tr>")
  16:Next field
  17:output.WriteLine("</table>")
  18:End Sub
  19:End Module
End Script Code

Also, some users ask to me about remove the copyright in the bottom of webpart, I think that csegScript is a tool for developpers and is not thought, for end users.And in a future I will open the code.

Note: If you want to share your small tricks, suggest improvements with csegScript, csegRollUp or others webparts please uses the forum or comments. I always respond.

(the cut and paste code is in the forum :-) )
 

Monday, August 08, 2005 8:31:53 PM (Hora de verano romance, UTC+02:00)   #    Comments [0]   csegScript  | 

imagine test your webparts without compile, imagine an VBA for sharepoint, that was what I imagined yesterday and this is here... I have desire to say Yeahhhhh

Screenshoots (better an image than thousand words)

csegScript showing code

 

csegScriptWebPart custom Properties

Show Code – Show the code with line numbers to identificate errors
Run Code – The code is executed when you applies changes
Script References – Write here the references of the project (.dll files)

If the file is in windows\system or in the assembly the path is not necesary so if you need a .dll (as Microsoft.SharePoint.dll) you need include the full path in the server, separe each reference with semicolon

Script Code – Visual Basic .NET based code, (Visual studio for applications code)

Separate lines with “\\” double backslash, internally they are replaced by carriage returns.

csegScript properties



Sample script code

This sample code shows all lists in the current site and in subsites with a link to the default list view. (the source is included in the zip file)

   1:  imports Microsoft.SharePoint
   2:  imports Microsoft.SharePoint.Utilities
   3:  imports Microsoft.SharePoint.WebPartPages
   4:  imports Microsoft.SharePoint.WebControls
   5:  imports System.Web.UI
   6:  Module Script
   7:  Public Sub Main()
   8:     Dim site As SPSite = SPControl.GetContextSite(System.Web.HttpContext.Current)   
   9:     Dim allSites As SPWebCollection = site.AllWebs
  10:     Dim subSite As SPWeb
  11:     For Each subSite In allSites
  12:        Dim allSiteLists As SPListCollection = subSite.Lists
  13:        Dim subSiteList As SPList
  14:        For Each subSiteList In  allSiteLists
  15:           output.WriteLine("<IMG SRC='"+subSiteList.ImageUrl+"'></IMG>" & " " & _
                                 SPEncode.HtmlEncode(subSite.Name) & " :: " & _
                                 "<A HREF='" + subSiteList.DefaultViewUrl & "'>" & _
                                 SPEncode.HtmlEncode(subSiteList.Title) & _
                                 "</A><BR>")
  16:        Next subSiteList
  17:     Next subSite
  18:  End Sub
  19:  End Module
 
 

Special

There is two special keys (output and _this)

output (internally is an HtmlTextWriter object) that you can use in the script code.

Sample: output.WriteLine(“Hello Sharepoint Developpers”)

_this is a reference to the webpart (internally is a WebPart object)

Sample: output.WriteLine(_this.Title) 
 

Usage in six steps….

1.- Unmark “Run Script”  and mark “Show Script Source”
2.- Put your code in the properties window, after each line put \\ instead of newline.
3.- Add your system references in the Script References, separated by semicolon (remenber the full path if they are not in assembly)
4.- Apply changes
5.- If there are errors you can view the error description, line number and line text, solve the problems and apply changes.



6.- When all is correct mark “Run Script” and VOILA….

 

Download csegScriptWebPart.zip (7,91 KB)
Thursday, August 04, 2005 11:10:41 PM (Hora de verano romance, UTC+02:00)   #    Comments [1]   csegScript  | 

Page 1 of 1 in the csegScript category

Copyright © 2008 Carlos Segura. All rights reserved.