CategorySet cs = new CategorySet();
List<string> categoriesName = new List<string>();
foreach (ElementId elementId in new FilteredElementCollector(viewSheet.Document, viewSheet.Id).WhereElementIsNotElementType().ToElementIds()) {
Category elementCategory = doc.GetElement(elementId).Category;
if (!categoriesName.Contains(elementCategory.Name)) {
categoriesName.Add(elementCategory.Name);
cs.Insert(elementCategory);
}
}