Beispiel #1
0
        public static ListItem GetCamlListItem(this XElement xmlNode)
        {
            ListItem item = new ListItem();
            item.Fields =
                (from att in xmlNode.Attributes()
                 where att.Name.LocalName.StartsWith("ows_")
                 select new KeyValuePair<string, string>(
                     att.Name.LocalName.Substring("ows_".Length),
                     att.Value)).ToList();

            item.ID = Int32.Parse(item["ID"]);
            return item;
        }
Beispiel #2
0
        /// <summary>
        /// Try to fill a ListItem object from a datarow
        /// </summary>
        /// <param name="item">the ListItem object</param>
        /// <param name="row">the DataRow object</param>
        /// <param name="e">the first exception of conversion or null for success conversion</param>
        private void MapDataRowToListItem(DataRow row, ListItem item, out Exception e)
        {
            if (item == null)
                throw new ArgumentNullException("item");

            if (row == null)
                throw new ArgumentNullException("row");

            e = null;

            if (item.Fields == null)
            {
                item.Fields = new List<KeyValuePair<string, string>>();
            }

            foreach (DataColumn column in row.Table.Columns)
            {
                string fieldName = GetServerColumnFromClientColumn(column.ColumnName);

                if (fieldName == null)
                    continue;

                if (this.DataColumns.Count > 0 &&
                    !this.DataColumns.Contains(fieldName))
                    continue;

                string fieldValue;
                if (column.DataType == typeof(DateTime) && row[column] != null)
                    //fieldValue = ((DateTime)row[column]).ToString("yyyy-MM-ddTHH:mm:ssZ");
                //FIX FOR DBNULL DATETIME
                {
                    if (row[column] is DBNull)
                        fieldValue = (DateTime.Now).ToString("yyyy-MM-ddTHH:mm:ssZ");
                    else
                        fieldValue = ((DateTime)row[column]).ToString("yyyy-MM-ddTHH:mm:ssZ");
                }
                //END OF FIX FOR DBNULL DATETIME
                else
                    fieldValue = row[column].ToString();

                item.Fields.Add(new KeyValuePair<string, string>(fieldName, fieldValue));
                // FIX: Is there any columns that must not mapped to fields?
                // this maybe result to error from the sharepoint-side
            }
        }
Beispiel #3
0
        /// <summary>
        /// Try to fill a DataRow object from a list item
        /// </summary>
        /// <param name="item">the ListItem object</param>
        /// <param name="row">the DataRow object</param>
        /// <param name="e">the first exception of conversion or null for success conversion</param>
        private void MapListItemToDataRow(ListItem item, DataRow row, out Exception e)
        {
            e = null;

            if (item == null)
                throw new ArgumentNullException("item");

            if (row == null)
                throw new ArgumentNullException("row");

            foreach (KeyValuePair<string, string> cell in item.Fields)
            {
                DataColumn col = null;
                string columnName = GetClientColumnFromServerColumn(cell.Key);

                if (columnName == null)
                    continue;

                if (row.Table.Columns.Contains(columnName))
                    col = row.Table.Columns[columnName];

                try
                {
                    if (col != null)
                    {

                        if (col.DataType == typeof(String))
                            row[col] = cell.Value;
                        else if (col.DataType == typeof(int))
                            row[col] = ParseIntOrLookupID(cell.Value); // lookup
                        else if (col.DataType == typeof(Guid))
                            row[col] = new Guid(cell.Value);
                        else if (col.DataType == typeof(DateTime))
                            row[col] = DateTime.Parse(cell.Value);
                        else if (col.DataType == typeof(Boolean))
                            row[col] = (cell.Value == "1" ? true : false);
                        else if (col.DataType == typeof(float) || col.DataType == typeof(double))
                            row[col] = ParseFloat(cell.Value);
                        else
                            row[col] = cell.Value;
                    }
                }
                catch (Exception ex)
                {
                    if (e == null)
                        e = ex;
                    e = new Exception(ex.Message + " || LLLLLLL " + row.Table.TableName + row.Table.Columns[columnName].DataType.ToString() + "," + col.ColumnName + "," + cell.Value.ToString() + "," + col.DataType.ToString() + "," + cell.Key + "," + cell.GetType().ToString() + ",|" + CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator + ",|" + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="inserts"></param>
        /// <param name="updates"></param>
        /// <param name="deletes"></param>
        /// <param name="connection"></param>
        /// #UPLOAD 3
        public void Update(DataTable changes, SpConnection connection, out Collection<SyncConflict> errors)
        {
            errors = new Collection<SyncConflict>();

            if (changes == null)
                throw new ArgumentNullException("changes");

            if (connection == null)
                throw new ArgumentNullException("connection");

            int _batchSize = 25;
            int segmentsCount = (int)Math.Round( Math.Ceiling((double)changes.Rows.Count/ _batchSize),0);

            if (IgnoreColumnsOnUpdate != null)
            {
                // case to be handled
                // cannot remove Sharepoint ID.
                // cannot remove Primary Key of DataTable?

                foreach (string ignoredColumn in IgnoreColumnsOnUpdate)
                {
                    string clientColumn = GetClientColumnFromServerColumn(ignoredColumn);
                    if (clientColumn != null &&
                        changes.Columns.Contains(clientColumn))
                    {
                        changes.Columns.Remove(clientColumn);
                    }
                }
            }

            DataTable changesTotal = changes.Copy();

            for (int i = 0; i < segmentsCount; i++)
            {
                changes.Rows.Clear();

                CopyRows(changesTotal, changes, i * _batchSize, _batchSize);

                //SEND SEGMENT
                UpdateBatch batch = new UpdateBatch();

                string clientIdColumn = GetClientColumnFromServerColumn("ID");

                if (!changes.Columns.Contains(clientIdColumn))
                    throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Messages.ColumnIDNotContained, clientIdColumn));

                IDictionary<int, DataRow> IdMapping = new Dictionary<int, DataRow>();

                foreach (DataRow row in changes.Rows)
                {
                    UpdateItem u = batch.CreateNewItem();

                    switch (row.RowState)
                    {
                        case DataRowState.Added:
                            u.Command = UpdateCommands.Insert;
                            break;
                        case DataRowState.Deleted:
                            u.Command = UpdateCommands.Delete;
                            break;
                        case DataRowState.Modified:
                            u.Command = UpdateCommands.Update;
                            break;
                        case DataRowState.Unchanged:
                            continue;
                    }

                    if (u.Command == UpdateCommands.Delete)
                        row.RejectChanges();

                    if (u.Command != UpdateCommands.Insert)
                    {
                        if (!(row[clientIdColumn] is DBNull))
                        {
                            u.ListItemID = (int)row[clientIdColumn];
                        }
                        else
                        {
                            continue;
                        }
                    }

                    if (u.Command != UpdateCommands.Delete)
                    {
                        ListItem item = new ListItem();
                        Exception e;

                        MapDataRowToListItem(row, item, out e);
                        u.ChangedItemData = item;
                        if (e != null && SyncTracer.IsErrorEnabled())
                            SyncTracer.Error(e.ToString());
                    }

                    batch.Add(u);
                    IdMapping[u.ID] = row;

                    if (u.Command == UpdateCommands.Delete)
                        row.Delete();
                }

                if (batch.Count != 0)
                {
                    //try
                    //{
                        UpdateResults results = connection.UpdateListItems(this.ListName, batch);
                        // FIX: errors must be handled appropriately
                        foreach (UpdateResult r in results)
                        {
                            if (!r.IsSuccess())
                            {
                                if (!IdMapping.ContainsKey(r.UpdateItemID))
                                    throw new InvalidOperationException(
                                        String.Format(CultureInfo.CurrentCulture, Messages.NoIDMapping, r.UpdateItemID));

                                DataRow clientRow = IdMapping[r.UpdateItemID];
                                errors.Add(CreateSyncError(r, clientRow));
                            }
                        }
                    //}
                    //catch (Exception ex)
                    //{
                    ////usually connection error
                    //    foreach (UpdateItem item in batch)
                    //    {
                    //        if (!IdMapping.ContainsKey(item.ID))
                    //            throw new InvalidOperationException(
                    //                String.Format(CultureInfo.CurrentCulture, Messages.NoIDMapping, r.UpdateItemID));

                    //        DataRow clientRow = IdMapping[item.ID];
                    //        errors.Add(CreateSyncError(new UpdateResult(, clientRow));
                    //    }
                    //}
                }
                //END SEND SEGMENT
            }

            if (errors.Count == 0)
                errors = null;
        }