воскресенье, 11 сентября 2011 г.

Sort list of Views with sql method


this powershell script sort list of views without they deleting.


$SortedListUser = New-Object 'System.Collections.Generic.List[string]'
$MyPath = Read-Host "Path To File"
$MyFile = Get-Content $MyPath
foreach ($i in $MyFile) {
if($i -ne "")
{
   $SortedListUser.Add($i)
}
}
$SortedListDesc = New-Object 'System.Collections.Generic.List[string]'
for( $i = $SortedListUser.Count; $i -ge 0; $i-=1 )
{ $SortedListDesc.Add($SortedListUser[$i]) }
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString =
"Data Source=server;Initial Catalog=db ;Integrated Security=True"
$Connection.Open()
$TeamSitePrompt = Read-Host "What is the Team Site URL (i.e. http://test.com/teamsite)"
$ListNamePrompt = Read-Host "What is the List Name you want to sort"
$Web = Get-SPWeb $TeamSitePrompt
$List = $Web.Lists[$ListNamePrompt]
$Views = $List.Views
#Create a Sorted List to store the Views you want to Sort
$SortedList = New-Object 'System.Collections.Generic.List[string]'  
foreach ($CurrentItemUser in $SortedListUser)
{
foreach ($CurrentViewItem in $Views)
{

if($CurrentItemUser -eq $CurrentViewItem.Title)
{
  Write-Host $CurrentViewItem.Id
  $uslovie = $CurrentViewItem.Id
  $Command = New-Object System.Data.SQLClient.SQLCommand
  $Command.Connection = $Connection
  $Command.CommandText = "UPDATE WSS_Content.dbo.AllWebParts SET tp_CreationTime = GETDATE() Where tp_ListId ='"+$List.Id+"' AND tp_ID ='"+$CurrentViewItem.Id+"'"
  #$Command.CommandText
$Command.ExecuteNonQuery()
}
}
}
$Connection.Close()


Create txt  file, sorting name of views :
view1
view2
view3

Run script!

Add items to dropdown in a Infopath with sql data


First add a field, check "Repeating" checkbox and select this field in the drop down control from "Look up values in the form's data source".
Then using the following code, add the data.



 private string getConnectionString(SPWebApplication myWebApplication,string typeBD)
        {
            string ConnectionString = string.Empty;
            if (myWebApplication.Properties != null && myWebApplication.Properties.Count > 0 && myWebApplication.Properties.ContainsKey(typeBD))
            {
                ConnectionString = GetString(myWebApplication.Properties[typeBD]);
            }
            return ConnectionString;
        }
        private string GetString(object obj)
        {
            string str = "";
            if (obj != null)
                str = obj.ToString();
            return str;
        }

        public void FormEvents_Loading(object sender, LoadingEventArgs e)
        {
             string connection = "";
             using (SPSite site = new SPSite(this.ServerInfo.SharePointSiteUrl.ToString()))
             {
                 connection = getConnectionString(site.WebApplication, "Akcent");
                 string query = "";
                 query = @"SELECT
                                 [id]
                                ,[guid]
                                ,[code]
                                ,[caption]
                                ,[type_id]
                            FROM [Budget].[SACG].[v_ref_cfo_src]";
                 int field_sql = 3;//поле отображения
                 SetDataToDropDown(connection, query, "ValuesField", field_sql);
}
}

This method get the field with data and set you values!

private void SetDataToDropDown(string connection,string queryString,string fieldName,int field_sql)
        {
            DataSet dsLoc = GetDataFromSql(connection,queryString);
            XPathNavigator DOMMain = DataSources[""].CreateNavigator();
            XPathNavigator Loc = DOMMain.SelectSingleNode("/my:myFields/my:"+fieldName, this.NamespaceManager);
            Loc.SetValue("");
            XPathNavigator templateNode = Loc.Clone();
            foreach (DataRow dr in dsLoc.Tables[0].Rows)
            {
                XPathNavigator newNode = templateNode.Clone();
                string value = dr[field_sql].ToString();
                newNode.SetValue(value);
                Loc.InsertAfter(newNode);
            }
            Loc.DeleteSelf();
        }
        private DataSet GetDataFromSql(string connection_string, string queryString)
        {
            DataSet dsLoc = new DataSet();
            SPSecurity.RunWithElevatedPrivileges(delegate()
           {
               using (SqlConnection connection = new SqlConnection(connection_string))
               {
                   connection.Open();
                   SqlCommand command = new SqlCommand(
                       queryString, connection);
                   SqlDataAdapter sqlData = new SqlDataAdapter(command);
                   sqlData.Fill(dsLoc);
                   connection.Close();
               }
           });
            return dsLoc;
        }

It's all!

InfoPath 2010 and new SPSite() error


Hello, if you work with infoPath and try create an new SPSite object you have an error with "/".
This means that, you infoPath form dont have need permissions in security level.
Do next step: Go to "File"

click "Advanced form options":
Set "Full trast".
Now, you are can create an new SPSite!